There are 2 scenarios for this issue:
with using preventDefault()
Without using preventDefault()
I am changing the active class using jQuery.
What happens when I use preventDefault():
As you can see, I clicked on About us link, it shows active after clicking but the content is not displaying.
What happens when I don't use preventDefault():
As you can see, here I clicked on About us link, content is displaying properly but the active class still there in a Home link.
How to sort out this issue? I found this link:
How to change active class while click to another link in bootstrap use jquery?
but here the issue is not resolved. I tried every jQuery but it didn't work.
Code:
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar2">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="Default.aspx"><img src="images/layout/check1.png" style="width:310px;padding-left:82px;height:60px;" class="img-responsive" alt="dubaiexporters.com"/>
</a>
</div>
<div id="navbar2" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active" style="font-family:Merriweather;"><a href="Default.aspx">Home</a></li>
<li class="menu" style="font-family:Merriweather;"><a href="advertize.aspx">Advertise</a></li>
<li class="dropdown" style="font-family:Merriweather;">
<a href="exhibition.aspx" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Exhibitions <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li style="font-family:Merriweather;"><a href="dubaiexhibitions.aspx">Trade fairs in U.A.E</a></li>
<li style="font-family:Merriweather;"><a href="exhibition.aspx">Trade fairs worldwide</a></li>
<li style="font-family:Merriweather;"><a href="addexhibition.aspx">Add Your Event</a></li>
</ul>
</li>
<li style="font-family:Merriweather;"><a href="subscribe.aspx">Subscribe</a></li>
<li style="font-family:Merriweather;"><a href="member_benefits.aspx">Memberships</a></li>
<li style="font-family:Merriweather;"><a href="aboutus.aspx">About us</a></li>
<li style="font-family:Merriweather;"><a href="news.aspx">News</a></li>
<li style="font-family:Merriweather;"><a href="contactus.aspx">Contact us</a></li>
<li style="font-family:Merriweather;"><a href="media_partners.aspx">Partners</a></li>
<li style="font-family:Merriweather;"><a href="login.aspx"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
<a href="addlisting.aspx" target="_blank" class="floatingbanner"></a>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container-fluid -->
</nav>
Jquery:
$(document).ready(function () {
$('.nav li a').click(function (e) {
$('.nav li.active').removeClass('active');
var $parent = $(this).parent();
$parent.addClass('active');
//e.preventDefault();
});
});