Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery click anything but div and children

Tags:

jquery

I have a login window I am trying to fade out by clicking anywhere but in the div (and the login button which toggles the fade in and fade out)

 jQuery(document).ready(function ($) {
    $('.login').click(function () {
        $('#modal_login').fadeToggle('fast');
    });

    $('body').click(function (e) {
        if (!$(e.target).is('.login, #modal_login *')) {
            $('#modal_login').fadeOut('fast');
        }
    });
});

It all seems pretty straight forward, but my issue is with the #modal_login and it's children. If you click anywhere in the #modal_login div, it fades out. I've tried #modal_login > * as well with no luck.

The code works nicely, with the ".login" class button.

So my question is, am I even using the right syntax for what I'm trying to accomplish (to not have the div fade out when clicked on or click on any of it's children)? It seems legit?

like image 504
user2624620 Avatar asked Dec 05 '25 05:12

user2624620


1 Answers

One way around this is to prevent clicks within the modal from bubbling beyond the modal. Something like this:

$("#modal_login").click(function(e){
    e.stopPropagation();
});

Now clicks within the modal will not bubble to the outer handler which does the fadeOut.

like image 189
James Montagne Avatar answered Dec 07 '25 18:12

James Montagne



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!