Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2009, 03:02 PM   PM User | #1
actionM
New to the CF scene

 
Join Date: Jan 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
actionM is an unknown quantity at this point
addEventListener problem

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
actionM is offline   Reply With Quote
Old 01-30-2009, 03:11 PM   PM User | #2
actionM
New to the CF scene

 
Join Date: Jan 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
actionM is an unknown quantity at this point
*edit* ooopssssssssssssssssssssssss

Last edited by actionM; 01-30-2009 at 03:14 PM.. Reason: i'm an idiot
actionM is offline   Reply With Quote
Old 01-30-2009, 03:37 PM   PM User | #3
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
Quote:
Code:
darkBoxImg[i].addEventListener("mousemove",function(){showBigImg(i)},false);
i is undefined.

You can do something like this:
Code:
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
itsallkizza is offline   Reply With Quote
Old 01-31-2009, 12:13 AM   PM User | #4
actionM
New to the CF scene

 
Join Date: Jan 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
actionM is an unknown quantity at this point
I tried that bottom for loop but it didn't work. And I'm really not sure what to do with the top function.
actionM is offline   Reply With Quote
Old 01-31-2009, 06:26 AM   PM User | #5
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
Change highlighted:
Code:
for (i = 0; i < darkBoxImg.length; i++) {
   darkBoxImg[i].addEventListener("mousemove",function(){showBigImg(i)},false);
}
to:
Code:
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.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Old 01-31-2009, 11:02 AM   PM User | #6
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
Quote:
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.
__________________
Feel free to e-mail me if I forget to respond ;)
ohsosexybrit@gmail.com
itsallkizza is offline   Reply With Quote
Old 01-31-2009, 03:07 PM   PM User | #7
actionM
New to the CF scene

 
Join Date: Jan 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
actionM is an unknown quantity at this point
rangana, thanks, that works. Now I just have to figure out why it works lol. And thanks for reminding me about the IE thing.
actionM is offline   Reply With Quote
Old 02-01-2009, 09:16 AM   PM User | #8
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
Quote:
Originally Posted by actionM View Post
Now I just have to figure out why it works lol.
It splits strings from the image's source to determine the number:
Code:
showBigImg(Number(this.src.split('pics/')[1].split('small')[0])-1);
  • This part:
    Code:
    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:
    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.


Hope that makes sense.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Old 02-01-2009, 03:02 PM   PM User | #9
actionM
New to the CF scene

 
Join Date: Jan 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
actionM is an unknown quantity at this point
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.
actionM is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:09 AM.


Advertisement
Log in to turn off these ads.