I've been fiddling around with this for a while, but can't really make it work as supposed to... So, I would like to close the navigation when I click outside the toggle button, I have tried few solutions with stopPropagation() but didn't really understand how it works. Could someone please provide a solution and maybe a brief explanation how this should be implemented? Thanks a lot for any help!
I have tried the solution with stopPropagation from
Click outside menu to close in jquery
but it works only the first time, and after it hides the menu it does not toggle again..
$(".app-toggle-menu").on('click', function() {
$(".app-main-navigation").toggleClass("app-main-navigation--expanded");
});
$('html').click(function() {
$(".app-main-navigation").hide();
});
$('.app-toggle-menu').click(function(event){
event.stopPropagation();
});
.app-main-navigation {
background: grey;
display: none;
}
.app-main-navigation--expanded {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="app-toggle-menu">
toggle
</button>
<nav class="app-main-navigation">
<ul>
<li>one</li>
<li>two</li>
</ul>
</nav>