$(document).ready(function () {
$('.nav-btn').on('click', function (e) {
// Implementing a feature to prevent document click when nav item is clicked.
e.stopPropagation()
$('.nav-btn').removeClass('active');
var subMenu = $(this).parent().find('.sub-menu')
if (!$(".sub-menu").is(":visible")) {
$(this).parent().find('.sub-menu').slideToggle();
}
$(this).toggleClass('active');
});
// Handling toggle of Sub Menu and removing active class on click outside the menu
$(this).on('click', function (event) {
$('.nav-btn').removeClass('active');
$('.nav-btn').parent().find('.sub-menu').slideToggle();
});
// Ensuring View stays open when Sub Items are clicked
$('.sub-menu').on('click', function (e) {
e.stopPropagation()
})
});
Just a few modifications to prevent document click when clicking on the nav item and manage additional functionalities as shown in the code above.
You can also check out a Plunker example here.