...

View Full Version : addEventListener problem



actionM
01-30-2009, 04:02 PM
Hey, I'm new here! On to the question. Why are the 2 examples of JavaScript that I have below, not exactly the same? The first one works as I wanted it to; it makes each image have a mouseover state that triggers an alert that displays a 0, 1, 2, or 3 for each image. However, the second example doesn't do that at all. The alert for each image is 4.

Why are they different? How can I get the second JS example do exactly as the first? Also, any other JS tips you can give to make my code better would be appriciated. Thank you.

Here is the relevant HTML:


<div id="darkBox">
<a href="pics/1big.jpg"><img src="pics/1small.jpg" /></a>
<a href="pics/2big.jpg"><img src="pics/2small.jpg" /></a>
<a href="pics/3big.jpg"><img src="pics/3small.jpg" /></a>
<a href="pics/4big.jpg"><img src="pics/4small.jpg" /></a>
</div><!-- end of id darkBox -->


Javascript 1:


function showBigImg(x) {
alert(x);
}

var darkBoxImg = document.getElementById("darkBox").getElementsByTagName("img");

darkBoxImg[0].addEventListener("mousemove",function(){showBigImg(0)},false);
darkBoxImg[1].addEventListener("mousemove",function(){showBigImg(1)},false);
darkBoxImg[2].addEventListener("mousemove",function(){showBigImg(2)},false);
darkBoxImg[3].addEventListener("mousemove",function(){showBigImg(3)},false);



Javascript 2:


function showBigImg(x) {
alert(x);
}
var darkBoxImg = document.getElementById("darkBox").getElementsByTagName("img");

var i = 0
for (i = 0; i < darkBoxImg.length; i++) {
darkBoxImg[i].addEventListener("mousemove",function(){showBigImg(i)},false);
}

actionM
01-30-2009, 04:11 PM
*edit* ooopssssssssssssssssssssssss

itsallkizza
01-30-2009, 04:37 PM
darkBoxImg[i].addEventListener("mousemove",function(){showBigImg(i)},false);


i is undefined.

You can do something like this:


function addEventHandler(to_element,event,handler)
{
if (typeof(handler)=="string") handler = Function(handler);
var f = event.substr(0,2);
var e = (f=="on"||f=="On"||f=="ON"||f=="oN") ? event.substr(2) : event;
to_element.addEventListener ? to_element.addEventListener(event,handler,false) : to_element.attachEvent("on"+event,handler);
}

for (var i=0;i<darkBoxImg.length;i++)
{
addEventListener(darkBoxImg[i],"mousemove","showBigImg("+i+")");
}

actionM
01-31-2009, 01:13 AM
I tried that bottom for loop but it didn't work. And I'm really not sure what to do with the top function.

rangana
01-31-2009, 07:26 AM
Change highlighted:


for (i = 0; i < darkBoxImg.length; i++) {
darkBoxImg[i].addEventListener("mousemove",function(){showBigImg(i)},false);
}


to:


darkBoxImg[i].addEventListener("mousemove",function(){showBigImg(Number(this.src.split('pics/')[1].split('small')[0])-1)},false);


Also, I hope you are aware that addEventListener works on FF only. If you want it to work on IE, you should make use of attachEvent().

Hope that helps.

itsallkizza
01-31-2009, 12:02 PM
I tried that bottom for loop but it didn't work. And I'm really not sure what to do with the top function.
The first function is being called by the for loop.

actionM
01-31-2009, 04:07 PM
rangana, thanks, that works. Now I just have to figure out why it works lol. And thanks for reminding me about the IE thing.

rangana
02-01-2009, 10:16 AM
Now I just have to figure out why it works lol.

It splits strings from the image's source to determine the number:


showBigImg(Number(this.src.split('pics/')[1].split('small')[0])-1);



This part:


this.src.split('pics/')[1]


...splits the image's source with pics/ string. This returns an array. The index of 1 means to get the 2nd index of the array, which now contains:


[NUMBER_HERE]small.jpg



The second part:


this.src.split('pics/')[1].split('small')[0]


...splits the above-mentioned string with the small string as the delimeter. It returns an array, and the first index is the number part.



With that said, it now gets the number from the image's source (but still a string). So, the purpose of placing all of the arguments inside the Number method is to turn the string into a number so that we could subtract it with 1.




Hope that makes sense.

actionM
02-01-2009, 04:02 PM
Thanks, I was having trouble looking up each component of that lengthy step. I still haven't found the best resource on the web for looking things like that up. I'm kinda looking for a site like php.net, but for javascript.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum