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 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'this' object in form elements

    Hi,

    I have seen some code around the place using the 'this' object differently to what I've seen in tutorials/references around the place.

    I've read that 'this' points to the current window object, but I've also seen people doing this:

    <input type="input" name="foo" onchange="setValue(this);">

    Is this correct? When used in that context does the this object point at the input 'foo'?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Yes indeed. The object foo and its value is passed to the function setValue(), e.g.

    function setValue(which) {
    var theValue = which.value;
    theValue ++ // or whatever
    }

  • #3
    New Coder
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that Makes my page much simpler to code and understand.

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FYI: This (no pun intended) is very helpful when you have several <input>s that call the same function. For example you can type onClick="myFunction(this.type, this.name)" and then just copy-paste into each <input> field that would use that function...


  •  

    Posting Permissions

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