Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return false on parent only

Tags:

html

jquery

I am using return false to stop a parent link firing. The parent link is used to open the dropdown.

The problem I have is this return false is also setting return false for the children in the dropdown.

How can I set only the parent link to false, and all the children links to true?

jQuery:

$('#account').on('click', function() {
    $('#account-options').fadeIn('slow'); 
    $(document).one('click', function() { 
        $("#account-options").fadeOut('slow'); 
    });
    return false;
});

$("#account-options").on('click', function() { 
    return false; 
});

HTML:

<li id="account"><a href="/account">Your Account</a>
    <ul id="account-options">
        <li><a href="">test1</a></li>
        <li><a href="">test2</a></li>
    </ul>
</li>

Thank you.

like image 894
ccdavies Avatar asked Dec 16 '25 17:12

ccdavies


1 Answers

You're running into event propagation. When you click on one of the <a> elements in #account-options it bubbles up to #account-options, executing its click event handler, then in turn up to #account, executing its event handler, both of which prevent the default behaviour of the event (in this case, following the link). You need to stop the event propagation at the <a> level:

$('#account-options a').on('click', function(e) {
   e.stopPropagation(); 
});

jsFiddle demo

like image 95
Anthony Grist Avatar answered Dec 19 '25 15:12

Anthony Grist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!