View Full Version : What does the keyword "this" mean
08-25-2010, 02:13 AM
I've seen it in quite a few websites and I'd like to know how it works and what it's used for.
An example I've seen it in is:
<td onmouseover="this.bgcolor='#aaaaaa';" onmouseout="this.bgcolor='#ffffff';">
Any help would be appreciated.
08-25-2010, 02:57 AM
this simply means "this object" or "the current object".
So in the code you showed, it refers to the <td> object. More correctly, it refers to the DOM object that the HTML <td> tag was rendered as. But we don't need to get that pedantic (despite my moniker). Just think of it as applying to the current tag.
Yes, there are other circumstances where it might refer to objects that aren't the result of HTML tags, but now we are getting pedantic.
08-25-2010, 05:20 AM
Ok, so basically if you use the "this" keyword it alowes you to alter the properties and methods of the tag you use it on. Like if you say:
<input type="text" onmouseover="this.value='Type your text here';">
it would manipulate the textbox to say "Type your text here" whenever you mouse over it
One more thing, can you use other event handlers other than the onmouseover and the onmouseout such as the onclick event, or the onsubmit event?
08-25-2010, 09:11 AM
Oh, yes. Any event handler that is supported by the browser for the tag in question.
MSIE has dozens and dozens of events supported that no other browser has, and even all of them could use this.
Note that, if you write the code correctly, the this doesn't have to appear "inline" in the tag. You can attach event handlers to tags with functions that appear (for example) in the <head> of the page.
08-25-2010, 06:24 PM
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.