Source: https://stackoverflow.com/questions/152975/how-do-i-detect-a-click-outside-an-element/3028037#3028037
$(document).click(function(event) { if(!$(event.target).closest('#menucontainer').length) { if($('#menucontainer').is(":visible")) { $('#menucontainer').hide(); } } });