These forums are currently read-only due to receiving more spam than actual discussion. Sorry.

It is currently Sat Dec 02, 2017 4:15 pm Advanced search

getThisElement()

Do you think the HTML spec should do something differently? You can discuss spec feedback here, but you should send it to the WHATWG mailing list or file a bug in the W3C bugzilla for it to be considered.

getThisElement()

Postby PMB9 » Sat Jul 11, 2009 1:56 pm

I like to propose the new DOM method getThisElement().
Examples:
Code: Select all
<img onclick="alert(getThisElement().tagName)" src="click.png" alt="Click me!">

will return "img" on click.
Code: Select all
<script>
var hello = document.createElement("p");
hello.innerHTML = "Hello <em>World</em>!";
getThisElement().parentNode.appendChild(hello);
</script>

will create a new paragraph "Hello World!" in the parent node of script.
PMB9
<h3>
 
Posts: 50
Joined: Sat Jan 26, 2008 5:38 pm

Postby JAB Creations » Sun Jul 12, 2009 1:17 am

This is not a tutorial/learning forum, it's here for feedback on the HTML5 spec with WHATWG. You clearly don't seem to understand that because you're suggesting JavaScript on an HTML forum for something that already exists in JavaScript in the first place.

It's time you learned how to work with the existing JavaScript DOM...
http://www.howtocreate.co.uk/tutorials/ ... /dombasics
http://www.howtocreate.co.uk/tutorials/ ... mstructure

Also do not use innerHTML unless you explicitly intend to only write to an element. It is not reliable to work with no matter how much any one to the contrary will tell you.
User avatar
JAB Creations
<aside>
 
Posts: 566
Joined: Tue Mar 13, 2007 4:48 am
Location: Sarasota Florida, USA

Re: getThisElement()

Postby zcorpan » Fri Jul 24, 2009 8:31 am

PMB9 wrote:I like to propose the new DOM method getThisElement().
Examples:
Code: Select all
<img onclick="alert(getThisElement().tagName)" src="click.png" alt="Click me!">

will return "img" on click.

Use
Code: Select all
<img onclick="alert(this.tagName)" src="click.png" alt="Click me!">

PMB9 wrote:
Code: Select all
<script>
var hello = document.createElement("p");
hello.innerHTML = "Hello <em>World</em>!";
getThisElement().parentNode.appendChild(hello);
</script>

will create a new paragraph "Hello World!" in the parent node of script.
Use
Code: Select all
<script>
document.write("<p>Hello <em>World<\/em>!<\/p>";
</script>
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden

Postby JAB Creations » Fri Jul 24, 2009 10:04 am

JavaScript's this (keyword/operator?) is great and I highly recommend using it as zcorpan suggests.

However do not use document.write as it not only is incompatible with XHTML it also encourages the very bad practice of including scripts inside of the body element. Learn to use the DOM correctly and you'll quickly realize how the restrictions of poor coding practices were/would hold you back.
User avatar
JAB Creations
<aside>
 
Posts: 566
Joined: Tue Mar 13, 2007 4:48 am
Location: Sarasota Florida, USA


Return to Feedback on the Specs

Who is online

Users browsing this forum: No registered users and 0 guests