javascript - How to untoggle a dropdown by clicking elsewhere in the page - stopPropagation? -
i wonder if can resolve issue brought on while back.
i unable untoggle these dropdown menus clicking outside of button, or anywhere else on page.
please see jsfiddle.
i've seen folks using stoppropagaton() unsure how apply here.
any ideas how this?
my toggling code:
var cur = null; $(".toggle").click(function(e){ $('#nav ul:visible').hide(); if(cur == null || cur.currenttarget != e.currenttarget) { if(cur != null) { $(cur.currenttarget) .children('a:first').children('span').removeclass('fc-state-active'); } cur = e; $(cur.currenttarget) .children('a:first').children('span').addclass('fc-state-active'); $(cur.currenttarget) .children('ul').show(); } else { $(cur.currenttarget) .children('a:first').children('span').removeclass('fc-state-active'); cur = null; } });
i believe following should work you. utilizes jquery's focusout() function:
$(".toggle").click(function(){ $('#nav ul:visible').hide(); $('span.fc-state-active').removeclass('fc-state-active'); $(this).children('a:first').children('span').addclass('fc-state-active'); $(this).children('ul').show(); }).focusout(function(){ $('#nav ul:visible').hide(); $('span.fc-state-active').removeclass('fc-state-active'); }); and here's updated fiddle: jsfiddle
edit: following works in ff & chrome new fiddle: jsfiddle
$(".toggle").click(function(){ $('#nav ul:visible').hide(); $('span.fc-state-active').removeclass('fc-state-active'); $(this).children('a:first').children('span').addclass('fc-state-active'); $(this).children('ul').show(); hide = false; }); $(document).click(function(){ if(hide){ $('#nav ul:visible').hide(); $('span.fc-state-active').removeclass('fc-state-active'); } hide = true; }); reason: $(document).click() called after $(".toggle").click()
edit 2: working fiddle can found here: jsfiddle
var hide; $(".toggle").click(function(){ var active_span = $(this).children('a:first').children('span'); var active_ul = $(this).children('ul'); $(active_span).toggleclass('fc-state-active'); $("span.fc-state-active").not(active_span).removeclass('fc-state-active'); $(active_ul).toggle(); $("#nav ul:visible").not(active_ul).hide(); hide = false; }); $(document).click(function(){ if(hide){ $('#nav ul:visible').hide(); $('span.fc-state-active').removeclass('fc-state-active'); } hide = true; });
Comments
Post a Comment