Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What does the keyword "this" mean

    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.

    Thanks
    Last edited by megatromboneguy; 08-25-2010 at 01:17 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,947
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,947
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok thanks.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •