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:
Code:
<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:
Code:
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:
Code:
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);
}
Last edited by actionM; 01-30-2009 at 03:13 PM..
Reason: stupid title
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+")");
}
__________________
Feel free to e-mail me if I forget to respond ;) ohsosexybrit@gmail.com
...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:
Code:
[NUMBER_HERE]small.jpg
The second part:
Code:
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.
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.