How to hide a div when clicking OUTSIDE of the div

Use jQuery to hide a DIV when the user clicks outside of it

How to hide a div when clicking OUTSIDE of the div
How to hide a div when clicking OUTSIDE of the div

The problem is that I have links inside the DIV and when they no longer work when clicked.

use jquery hide a div when clicking OUTSIDE of the div

I don’t consider this to be a good solution since it lets people think it is okay to populate the window-object (= using global variables)

Just to add something to what @prc322 said, you can wrap your code with an anonymous function and have it called immediately. (function() { // … code })(); I don’t remember the name os this pattern, but it’s super useful! All your declared variables will reside inside the function and won’t pollute the global namespace.

Working for me:

Download file: Click outside div

        $(document).click(function (event) {
            var clickover = $(event.target);
            var $navbar = $(".navbar-collapse");               
            var _opened = $navbar.hasClass("in");
            if (_opened === true && !clickover.hasClass("navbar-toggle")) {      
                $navbar.collapse('hide');
            }
        });

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *