Ironank Ironank - 1 year ago 70
Javascript Question

Show div and then hide when clicking outside

I have this code:

$(function() {
$('#toggle4').click(function() {
return false;

Which works great and shows the '.toggle4' div but I want to hide it again when clicking outside/away from it.

So I added this:

$(document).click(function() {

Which works but it hides the div even when I click inside of the '.toggle4' div (it's an input box for a search form).

Any ideas? Thanks.

Answer Source

That's because when you click inside of .toggle4 that click event bubbles up the DOM and triggers the event you bound to the document. You should be able to fix that with something like:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download