I have a jQuery snippet that collapses the responsive navbar
(in mobile mode) when a menu item is clicked.
$('.navbar-fixed-top').click('li', function() {
$('.navbar-collapse').collapse('hide');
});
However, I also have a couple of menu items with drop-down sub-menus and they have the Bootstrap class .dropdown
assigned to them. I want the above function to exempt these dropdown menu items.
To that end, I would like to retrieve the class of the item being clicked in order to make a decision on whether to collapse the navbar.
I tried this but it didn't output anything.
$('.navbar-fixed-top').click('li', function() {
console.log($(this).class);
$('.navbar-collapse').collapse('hide');
});
I also tried this with similar results.
$('.navbar-fixed-top').click('li', function(event) {
console.log(event.target.class);
$('.navbar-collapse').collapse('hide');
});
What else can I try? Right now, my navbar collapses when the dropdown menu item is tapped on a mobile device even before the user has a chance to see the sub-items which is what I am trying to remedy.
In case anyone wants to take a look, here's the HTML:
<ul class="nav navbar-nav pull-right">
<li ng-class="{ active: isActive('/about')}"><a onClick="pagetop()" href="#about">ABOUT</a></li>
<li ng-class="{ active: isActive('/blog')}"><a onClick="pagetop()" href="#blog">BLOG</a></li>
<li><a onClick="pagetop()" href="#">PREMIUM</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">RESOURCES<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
<li><a href="javascript:;" onClick="centerAlignModal()" data-target="#contact" data-toggle="modal">CONTACT</a></li>
</ul>
id
for the class? – jkrishasClass
? – naveenevent.target
(from your last snippet) will give you the thing that was clicked. Just remove theli
event. – axhi