0

I have this piece of html:

<div id="parent">
     <div id="child></div>
</div>

and it looks graphically like this:

---------------------------
-                         -
-    #parent              -
-                         -
-         ----------      -
-         -        -      -
-         - #Child -      -
-         -        -      -
-         ----------      -
-                         -
---------------------------

How do I trigger an event in jQuery (roll over for example) that will work only when hovering on the #parent but will not work when hovering on the #child

Alon
  • 7,518
  • 17
  • 56
  • 99

4 Answers4

3

you can use event.stopPropagation()

question similar to it is jquery stop child triggering parent event

$("#parent").click(function(event){
  //do something with parent

});
$("#child").click(function(event){
  event.stopPropagation();

});
Community
  • 1
  • 1
Hemant Metalia
  • 27,928
  • 18
  • 70
  • 89
0

I think looking into event.stopPropagation would do the trick...

$("#someElement").click(function(event){
  event.stopPropagation();
  // do something
});  

http://api.jquery.com/event.stopPropagation/

Danny
  • 468
  • 2
  • 7
0

If you return false from an event handler, it will stop the event from bubbling up to the next ancestor.

http://jsfiddle.net/jbabey/CV76D/

$('#outer').mouseover(function () {
    $(this).addClass('green');
});

$('#inner').mouseover(function () {
    return false;
});
jbabey
  • 44,525
  • 12
  • 67
  • 94
0
$('#parent').on('click',function(e){
    if(e.target === document.getElementById('child')) return false;
    // your code 
})

However in my point of view it will be better to resort to .stopPropagation(), as others said

Dmitry Koroliov
  • 3,979
  • 2
  • 24
  • 36