PDA

View Full Version : this.id not working



cdoverlaw
06-23-2009, 04:15 PM
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.


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


function MoveUp(){
alert(this.id);
RefreshOrder("TRUE");
}

The MoveUp routine gives the message "undefined" in the alert

Any help would be appreciated.
Thanks

Jonathan

ShadowIce
06-23-2009, 04:42 PM
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:


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

Good luck! :)

~SI~