Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get jQuery to ignore parents on .click()

Sample code:

<div id="a">
     <div id="b">
          Click here
     </div>
</div>

<script>
    $('*').click(function() {
        alert($(this).attr('id'));                    
    });
</script>

When you click 'Click Here' it alerts twice, once with 'b' and then with 'a'.

I need to figure out how to get jQuery to ignore all the parents of where the user clicked and just alert, in this case, 'b'.

like image 377
David B. Avatar asked Dec 04 '22 11:12

David B.


1 Answers

Try this:

  $('*').click(function(e) {
        e.stopPropagation();
        // do something
    });

Here you can find documentation: http://api.jquery.com/event.stopPropagation/

like image 84
Haim Evgi Avatar answered Dec 06 '22 02:12

Haim Evgi