Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set active class to nav menu from twitter bootstrap

I'm new to the twitter bootstrap. Using there navigation menus . I'm trying to set active class to selected menu. my menu is -

<div class="nav-collapse">   <ul class="nav">     <li id="home" class="active"><a href="~/Home/Index">Home</a></li>     <li><a href="#">Project</a></li>     <li><a href="#">Customer</a></li>     <li><a href="#">Staff</a></li>     <li  id="broker"><a href="~/Home/Broker">Broker</a></li>     <li><a href="#">Sale</a></li>   </ul> </div> 

I tried following thing after googling on this that i have to set active class on each page from menu like as--

<script>   $(document).ready(function () {     $('#home').addClass('active');   }); </script> 

but problem for above is that i set home menu selected by default. Then it always get selected. Is there any other way to do this ? , or which i can generalize and keep my js in layout file itself?

After executing application my menu looks - enter image description here

after clicking on other menu item i get following result- enter image description here

And i added following scripts on Index view and Broker view ---

<script>   $(document).ready(function () {     $('#home').addClass('active');   }); </script>  <script>   $(document).ready(function () {     $('#broker').addClass('active');   }); </script> 

respectively.

like image 262
Priyanka Avatar asked Feb 26 '13 05:02

Priyanka


People also ask

How do I set bootstrap active class to NAV menu?

To set the active class to the navigation menu dynamically by scrolling or clicking on the navigation links, the active class is to be set on each section depending on the position of the webpage. To add methods and variables, JavaScript is used.

How do I make bootstrap navigation active?

To set an active class in your bootstrap navbar, you can use ng-controller(NavigationController) to set bootstrap navbar active class with AngularJS. To run a single controller outside ng-view. You can set class= “active” when the angular route is clicked.


2 Answers

You can use this JavaScript\jQuery code:

// Sets active link in Bootstrap menu // Add this code in a central place used\shared by all pages // like your _Layout.cshtml in ASP.NET MVC for example $('a[href="' + this.location.pathname + '"]').parents('li,ul').addClass('active'); 

It'll set the <a>'s parent <li> and the <li>'s parent <ul> as active.

A simple solution that works!


Original source:

Bootstrap add active class to li

like image 189
Leniel Maccaferri Avatar answered Sep 28 '22 08:09

Leniel Maccaferri


it is a workaround. try

<div class="nav-collapse">   <ul class="nav">     <li id="home" class="active"><a href="~/Home/Index">Home</a></li>     <li><a href="#">Project</a></li>     <li><a href="#">Customer</a></li>     <li><a href="#">Staff</a></li>     <li  id="demo"><a href="~/Home/demo">Broker</a></li>     <li id='sale'><a href="#">Sale</a></li>   </ul> </div> 

and on each page js add

$(document).ready(function () {   $(".nav li").removeClass("active");//this will remove the active class from                                        //previously active menu item    $('#home').addClass('active');   //for demo   //$('#demo').addClass('active');   //for sale    //$('#sale').addClass('active'); }); 
like image 45
Dakait Avatar answered Sep 28 '22 10:09

Dakait