0

Lets say that I want to turn the paragraph elements red when I mouseover the corresponding link elements.

Why would the following code not work and what is the simplest alternative?

for (i = 0; i < 100; i++) {   
$("a").eq(i).mouseover(function(){
$("p").eq(i).css({"color":"red"})  
})
}

Need simple explanation.

Ned Hulton
  • 477
  • 3
  • 12
  • 27

1 Answers1

1

You dont have to loop through the elements. Do like this,

$("a").mouseover(function() {
    $("p").eq($('a').index(this)).css("color", "red");
});
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53