I would like to know whether this is the correct way of hiding visible elements when clicked anywhere on the page.
$(document).click(function (event) {
$('#myDIV:visible').hide();
});
The element (div, span etc) shouldn't disappear when click event occurs within the boundaries of the element.
This following code example seems to work best for me. While you can use 'return false' that stops all handling of that event for the div or any of it's children. If you want to have controls on the pop-up div (a pop-up login form for example) you need to use event.stopPropogation().
Here #suggest_input in is the name of textbox and .suggest_container is the ul class name and .suggest_div is the main div element for my auto-suggest.
this code is for hiding the div elements by clicking any where in the screen. Before doing every thing please understand the code and copy it...
but you need to keep in mind these things as well. http://css-tricks.com/dangers-stopping-event-propagation/
As of jQuery 1.7 there's a new way to handle events. I thought I'd answer here just to demonstrate how I might go about doing this the "new" way. If you haven't, I recommend you read the jQuery docs for the "on" method.
Here we're abusing jQuery's selectors and bubbling of events. Note that I make sure I clean the event handler up afterwards. You can automate this behaviour with
$('.container').one
(see: docs) but because we need to do conditionals within the handler that isn't applicable here.Here is a working CSS/small JS solution based on the answer of Sandeep Pal:
Try it out by clicking the checkbox and then outside of the menu:
https://jsfiddle.net/qo90txr8/