jquery - Close the hamburger menu when you click outside of container -


i want have simple hamburger menu. i'm using jquery toggle open , close when hamburger clicked. user able close when click anywhere outside of div or menu.

the jsfiddle have written out. don't mind styling.

i use like

$(window).on('click', function(){     $('.responsive-menu').removeclass('expand'); }) 

but added, cannot open menu. please help. thank you!

the trick check if element clicked has parent .responsive-menu or .menu-btn.

if has'n of 2 , menu expanded, toggle!

updated fiddle

$(document).on("click", function(e){   if(      $(e.target).closest(".responsive-menu").length == 0 &&     $(".responsive-menu").hasclass("expand") &&     $(e.target).closest(".menu-btn").length == 0   ){     $('.responsive-menu').toggleclass('expand');   } }); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -