When you click on a DOM object, it fires an event, which means that it starts from the very specific object and bubbles up to the DOM tree until it reaches the document object. You can prevent the DOM tree bubble event by returning false .
$(document).click(function() { $('#dropMenu').hide(); }); $('#optionButton').click(function() { $('#dropMenu').show(); return false; });
Notice how I used the hide and show methods instead of css('visibility' , 'visible/hidden') . These two actually do slightly different things , nut, if you just want to hide the element, the hide method is the easiest way to do this in jQuery.
You can see a working example of this in jsFiddle .
source share