tags:

views:

30

answers:

1

I guess the question is in the title: I want to apply a style only to the littlest hovered element when the user clicks. How do do I choose this very element ?

Thanks.

+1  A: 

To put a style on the div and not have the parent affected, you can stop the bubble with event.stopPropagation(), like this:

$("div").click(function(e) {
  $(this).toggleClass("myClass");
  e.stopPropagation();
});

You can see a demo of this working here

For the hover case, you actually would want mouseover and mouseout instead of mouseenter and mouseleave (which .hover() binds to) in this case, like this:

$("div").mouseover(function(e) {
  $(this).addClass("myClass").parents().removeClass("myClass");
  e.stopPropagation();
}).mouseout(function() {
  $(this).removeClass("myClass");
});​

You can see this in action here

Nick Craver
I tried this: $('div').hover( function(e){ $('div').addClass('hovered') e.stopPropagation(); }, function(e){ $('div').removeClass('hovered') e.stopPropagation(); })But it applies the style to all the DIV of the page :(
Coronier
@Coronier - You're using `$("div")` inside the function, not `$(this)` like my example :) Take a look here, I think this is what you're after: http://jsfiddle.net/HsBDQ/1/
Nick Craver
I feel so stupid ... It (of course) works now. Thank you Nick for your patience.
Coronier