mpgn mpgn - 3 months ago 11
HTML Question

Set link of my navbar active with bootstap

I try to set active the element of my navbar when i click on the button.

My navbar is like that :

navbar

But when i click on the "a" for exemple, the "G" remains active.

My code is like that :

<div class="navbar span8" id="navbar">
<div class="navbar-inner">
<ul class="nav">
<li><a href="#a">A</a></li>
<li><a href="#b">B</a></li>
<li><a href="#c">C</a></li>
<li><a href="#d">D</a></li>
<li><a href="#e">E</a></li>
<li><a href="#f">F</a></li>
.....
</ul>
</div>
</div>


To set a class active the code is :

<li class="active"><a href="#f">F</a></li>


How can i do that with jQuery ?

Answer

You can set it active like this..

$('.nav li').click(function(){
    $('.nav li').removeClass('active');
    $(this).addClass('active');
})

Bootply