0

If i have the following markup:

<ul id="level1">
    <li>
        <a href="#">Test</a>
        <ul id="level2">
            <li>
                <a href="#">Test</a>
            </li>
        </ul>
    </li>
</ul>

Basically if i hover over level 1, i need it to do something. However when i hover over level2 it is triggering it because that is inside level1. Is there a way of making this only happen if i hover over level1.

An example of what i mean:

http://scottymeuk.in/Netr

The box on the left, is a parent of the box on the right. But i need the something to happen when hovering over the left box (parent), but not the right box (child).

Thank you.

Sorry if i have failed to explain my issue well enough.

Scottymeuk
  • 791
  • 1
  • 6
  • 15

3 Answers3

3

One solution would be to call event.stopPropagation on the child:

$("#parent").on("someevent", function(){
  // do stuff
});

$("#child").on("someevent", function(e){
  e.stopPropagation(); // prevent the event from bubbling to the parent.
});
James Montagne
  • 77,516
  • 14
  • 110
  • 130
2

Please refer to the event object documentation...

http://api.jquery.com/category/event-object/

event.stopPropagation();

is what you need

$('#level2').hover)function(e) {
    e.stopPropagation(); // stops events from triggering in children
},function (e) {}
zgr024
  • 1,175
  • 1
  • 12
  • 26
0

$('level1').hover(function(){ if(!IsMouseOver($(this).find(level2)){ do what you want it to do }; });

I haven't tested it yet but you get the logic. is mouse over level1? yes. if it's over level1 and its not over level2 do what I want

qualebs
  • 1,291
  • 2
  • 17
  • 34