Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery or javascript along hide element if visible when clicking on the body else where
    text
    copied!<p>I am trying to close a make shift drop down menu when someone clicks elsewhere on the site. Only if it's found to be open. However the below snippet seems to misfire and lock the browser. So I think I am coming at this wrong. Is there a better way to handle this notion?</p> <pre><code>$(document.body).click(function() { if($('#ztsWidgetMenu').is(':visible')) { $('#addDashWidgetBox').click(); } }); </code></pre> <p><strong>EDIT</strong></p> <p>I made a function to fire off on the click event now for both the element to open it, and the body click to call to, the bubbling stops with that however I ran into another problem with it now. In order to open the menu I need to click on the meun item atleast 2 times for it to open the menu. And if I click on the same button to close it then I am stuck with it open unless I click on the body else where now..</p> <pre><code>function openCloseWidgetMenu() { dashboardJSON.widgets.sort(sortJSONresultsByWidgetID); if(widgetMenuShowHide == "hide") { widgetMenuShowHide = "shown"; $.each(dashboardJSON.widgets, function(x) { if(dashboardJSON.widgets[x].show == "no"){$('#ztsWidgetMenu ul').append('&lt;li class="widgetMenuItems" rel="'+dashboardJSON.widgets[x].wigetID+'" widgetcolor="'+dashboardJSON.widgets[x].widgetColor+'"&gt;'+dashboardJSON.widgets[x].widgetTitle+'&lt;/li&gt;');} }); $('#ztsWidgetMenu').css({"display":"block", "position":"absolute", "left":($('#addDashWidgetBox').position().left-35) + "px", "top":($('#addDashWidgetBox').offset().top+$('#addDashWidgetBox').height()+10) + "px"}); } else { $('#ztsWidgetMenu ul li').remove(); $('#ztsWidgetMenu').hide(); widgetMenuShowHide = "hide"; } } $('#addDashWidgetBox').live({ click: function() { //e.preventDefault(); openCloseWidgetMenu(); }, mouseover: function() { $('#addWidetMenuBar').css({"background-position":"-0px -697px"}); $(this).css({"color":"#000"}); }, mouseout: function() { $('#addWidetMenuBar').css({"background-position":"-0px -662px"}); $(this).css({"color":"#5a5a5a"}); } }); $(document.body).click(function() { if($('#ztsWidgetMenu').is(':visible')) { openCloseWidgetMenu(); } }); </code></pre> <p><strong>Edit 2</strong></p> <p>I Have fixed my issue for the most part, except now.. when I click on the element I want to fire off the function to show the menu the body click registers at the same time so now its firing the same function off twice with one click.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload