Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function triggered when click outside an element in jQuery

I want to show the different elements in the loop when I click on body, except from the element #search-container form. It works, but only if I first click on the element #search-container form, and it only works once.

$('html').click(function(event) {
    $('#search-container form').click(function(event){
        event.stopPropagation();
    });
    $('html').on('click',function() {
        $('#small-search a').toggleClass('notvisible');
        $('#session').toggleClass('notvisible');
        $('#search-container').css('position','absolute');
        $('#search-container form').toggleClass('visible');
    });
});
like image 314
estellechvl Avatar asked Sep 17 '26 01:09

estellechvl


1 Answers

$('html').not("#search-container form").on('click',function(){
    $('#small-search a').toggleClass('notvisible');
    $('#session').toggleClass('notvisible');
    $('#search-container').css('position','absolute');
    $('#search-container form').toggleClass('visible');
});
like image 139
Mohammad Adil Avatar answered Sep 18 '26 14:09

Mohammad Adil



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!