Results 1 to 2 of 2

Thread: this.id not working

  1. #1
    Join Date
    May 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default this.id not working

    hi

    I use this code to create 2 images next to a string, these are going to be used for moving nodes up and down.

    Code:
    divTag.innerHTML = sName + '<img src="images/up.png" id="MoveUp'+ sName + '" onclick="MoveUp()"/><img src="images/down.png" id="MoveDown'+ sName + '" onclick="MoveDown()"/>';
    Unfortunately when i click on these images, and run the following code

    Code:
    function MoveUp(){
    	alert(this.id);
    	RefreshOrder("TRUE");
    }
    The MoveUp routine gives the message "undefined" in the alert

    Any help would be appreciated.
    Thanks

    Jonathan

  2. #2
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    id is a function in javascript itself used in all form elements. try id1. also. u cant automatically assume "this" is defined, because it is not. in order to define something, u have to give it something TO define. so we use "w" in the function AND the alert. WHY? because ONLY form elements can use "this". there ARE ways to use "this" in the function itself, but its WAY hard to do.

    So just use it like this:

    Code:
    <div id="mydiv" onClick="MoveUp(this);">Click me</div><script>function MoveUp(w){
    	alert(w);
    	RefreshOrder("TRUE");
    }</script>
    Good luck!

    ~SI~

Bookmarks

Posting Permissions

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