I am facing an issue with my navigation drop down menu. Currently, the pure CSS functionality requires users to click the link again to close the sub-menu.
What I want is for the sub-menu to close whenever a click occurs outside of it.
I attempted a solution using JavaScript, but it didn't work as expected:
$('*:not(.submenu > li > a)').click(function() {
$('nav .dropdown input').removeAttr('checked');
console.log('clicked outside sub-menu');
});
This code captures every click event, including those within .submenu > li > a
, causing the menu to close even when trying to open it.
The dropdown menu is controlled by a checkbox input in the CSS. Here's the CSS code for reference, although my issue lies in the JavaScript part:
Here is a fiddle containing the CSS code: https://jsfiddle.net/z3yj7pLs/
ul.dropdown {
position:fixed;
top:30px;
width:100%;
height:3em;
margin:0;
padding:0 10px;
color:#eee;
}
(remaining CSS properties remain unchanged...)