How to change active class while click to another link in bootstrap use jquery?
I have a html as sidebar, and use Bootstrap
.
<ul class="nav nav-list">
<li class="active"><a href="/">Link 1</a></li>
<li><a href="/link2">Link 2</a></li>
<li><a href="/link3">Link 3</a></li>
</ul>
I want to do some thing:
When I click
a link such as Link 3, the page content
will change to Link 3's content, and the Link 3 will have class active
, and remove the active clss in Link 1.
I think this can be implemented in jQuery
, and I have searched many question in SO, such as:
- How to keep an active link in nav list (twitter bootstrap)?
- Bootstrap CSS Active Navigation
I use this script:
$(document).ready(function () {
$('.nav li').click(function(e) {
$('.nav li').removeClass('active');
var $this = $(this);
if (!$this.hasClass('active')) {
$this.addClass('active');
}
//e.preventDefault();
});
});
But most of them use preventDefault
, this will prevent the continued action. So it can't change to Link 3's page content.
If I remove the preventDefault statement, when the page load Link 3's content, the active class will back to Link 1.
So Is there any way to solve this problem?
You are binding you click on the wrong element, you should bind it to the a
.
You are prevent default event to occur on the li
, but li
have no default behavior, a
does.
Try this:
$(document).ready(function () {
$('.nav li a').click(function(e) {
$('.nav li.active').removeClass('active');
var $parent = $(this).parent();
$parent.addClass('active');
e.preventDefault();
});
});
I am using bootstrap navigation
This did the job for me including active main dropdown's and the active children
$(document).ready(function () {
var url = window.location;
// Will only work if string in href matches with location
$('ul.nav a[href="' + url + '"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.nav a').filter(function () {
return this.href == url;
}).parent().addClass('active').parent().parent().addClass('active');
});
I had the same issue before. Try this answer here, it works on my site.
$(function(){
var current_page_URL = location.href;
$( "a" ).each(function() {
if ($(this).attr("href") !== "#") {
var target_URL = $(this).prop("href");
if (target_URL == current_page_URL) {
$('nav a').parents('li, ul').removeClass('active');
$(this).parent('li').addClass('active');
return false;
}
}
});
});
Source: It was original posted here how to set active class to nav menu from twitter bootstrap
guys try this is a perfect answer for this question:
<script>
$(function(){
$('.nav li a').filter(function(){return this.href==location.href}).parent().addClass('active').siblings().removeClass('active')
$('.nav li a').click(function(){
$(this).parent().addClass('active').siblings().removeClass('active')
})
})
</script>
If you change the classes and load the content within the same function you should be fine.
$(document).ready(function(){
$('.nav li').click(function(event){
//remove all pre-existing active classes
$('.active').removeClass('active');
//add the active class to the link we clicked
$(this).addClass('active');
//Load the content
//e.g.
//load the page that the link was pointing to
//$('#content').load($(this).find(a).attr('href'));
event.preventDefault();
});
});