Make DOM elements hoverable using Prototype
Here’s a small snippet I use when I want to change style based off of the :hover state of the element. Usually we could just use the :hover pseudo-class (apart from the fact that IE6 will only acknowledge :hover on link tags only). The solution is to use some javascript to add/remove a .hover class on the elements we want to hover over (I mostly use this for lists).
Eventobservewindow'load'function
$$'.hoverable > *'each
Eventobservee'mouseover'function
ElementaddClassNamee'hover';
;
Eventobservee'mouseout'function
ElementremoveClassNamee'hover';
;
;
;
To use the above script, you need to mark any parent elements as .hoverable to have any of it’s direct children gain and lose .hover on :hover, such as the follow:
First Item
Second Item
Last Item
This is also easily written using jQuery, though as I mostly deal with Rails applications without an explicit need to use jQuery, this is the Prototype version I use. A jQuery version of this snippet can be found in the post, Make DOM elements hoverable using jQuery.

Leave a Reply