Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *edit* ooopssssssssssssssssssssssss
    Last edited by actionM; 01-30-2009 at 03:14 PM. Reason: i'm an idiot

  • #3
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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

  • #4
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried that bottom for loop but it didn't work. And I'm really not sure what to do with the top function.

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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

  • #6
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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

  • #7
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    rangana, thanks, that works. Now I just have to figure out why it works lol. And thanks for reminding me about the IE thing.

  • #8
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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

  • #9
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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