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.
Thanks, Thomas. I'm new to JS and I've been looking crazy for a solution to my problem. Yours helped.
I've used jquery to make a Login-box that slides down. For best user experience I desided to make the box disappear when user clicks somewhere but the box. I'm a little bit embarrassed over using about four hours fixing this. But hey, I'm new to JS.
Maybe my code can help someone out:
Try this:
Try this, it's working perfect for me.
Just 2 small improvements to the above suggestions:
stop propagation on the event that triggered this, assuming its a click
What you can also do is:
If your target is not a div then hide the div by checking its length is equal to zero.