views:

35

answers:

2

I am trying to add a highlight color to a class like this

    $(".common_box").hover(function(){
        $(".common_box").addClass("hover_me");
    });

this works but why doesnt this

    $(".common_box").hover(function(){
        $(".common_box").toggleClass("hover_me");
    });

when i hover over this nothing happens

is there no unhover to put to remove the class hover_me when they move away from the hover

+2  A: 

There's currently a bug in some situations where the mouse enter/leave events are firing twice, so it is working, but it's double toggling each in/out, so no net effect...for now, to be safe:

$(".common_box").hover(function(){
    $(this).addClass("hover_me");
}, function(){
    $(this).removeClass("hover_me");
});

This is a bug (I believe) because of the .live() changes to support .hover(), which is causing some unwanted side-effects, you can be explicit like above to be 100% safe, so if each handler runs multiple times, at least for your purposes, it's alright.

Nick Craver
+1  A: 

Try this

  $(".common_box").mouseover(function() {
    $(this).addClass("hover_me");
  }).mouseout(function(){
    $(this).removeClass("hover_me");
  });
jcubic
You want to stick with `mouseenter` and `mouseleave` (what `.hover()`'s 2 function arguments map to) here, as they won't be fired when entering/leaving child elements within .common_box.
Nick Craver