0

Currently, I'm looping through all my images and making their onmouseover/onmouseout events change their image with a for-loop.

 var arrows = document.getElementsByClassName('arrow');

 for (var i = 0; i < arrows.length; i++) {
     arrows[i].onmouseover = arrows[i] + ".src = 'images/downarrow_hover.gif';";
     arrows[i].onmouseout = arrows[i] + ".src = 'images/downarrow.gif';";
 }

It's not working, am I doing this right?

Bryan Menard
  • 13,234
  • 4
  • 31
  • 47
ffs82defxp
  • 13
  • 4

1 Answers1

0

You have to assign functions to onmouseover and onmouseout.

var arrows = document.getElementsByClassName('arrow');

for (var i = 0; i < arrows.length; i++) {
    arrows[i].onmouseover = function () {
        this.src = 'images/downarrow_hover.gif';
    };

    arrows[i].onmouseover = function () {
        this.src = 'images/downarrow.gif';
    };

For what it's worth and if you can afford to, you should look into jQuery which makes it a breeze to do a such thing:

$('.arrow').hover(
    function () { $(this).attr('src', 'images/downarrow_hover.gif'); },
    function () { $(this).attr('src', 'images/downarrow.gif'); });
Bryan Menard
  • 13,234
  • 4
  • 31
  • 47
  • thank you, apparently i had forgotten the very intuitive concept that events execute functions. as for the jquery, I now realize it's better to just simply use css background-image positioning on a css sprite for this type of thing. – ffs82defxp Aug 27 '11 at 23:59