Because you're using an ID, not a class. IDs must be unique on a page, while classes may be repeated. Just change all your divs to use a class="myselectordiv" instead of an id. Your jQuery selector would then change to:
jQuery('.myselectordiv')...
To get the rollover effect:
// Fade everything out first
jQuery('.myselectordiv').fadeTo(500, 0.2);
jQuery('.myselectordiv').hover(function () {
// Mouse enter, fade in
jQuery(this).fadeTo(500, 1);
}, function () {
// Mouse leave, fade out
jQuery(this).fadeTo(500, 0.2);
});
This binds two functions to your divs, one for mouseenter
and one for mouseleave
, and as you can see they do opposite fadeTos to each other.
Note: there's a subtle problem with this however, which you'll notice if you move your mouse over the divs rather quickly. Even after moving your mouse off the div, if it was still fading it, it will continue to complete the animation before it fades out again. This may be what you want, but if you move your mouse quickly between two divs, they will both continuously fade in and out afterwards because a long queue of animation effects have piled up. To prevent this, add a .stop()
before each fadeTo()
inside the hover:
jQuery(this).stop().fadeTo(500, 1);
Demo: http://jsfiddle.net/mm8Fv/