how to set active class to nav menu from twitter bootstrap

108,642

Solution 1

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');
});

Solution 2

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

Solution 3

I had the same problem... solved it by adding the code shown below to the Into "$(document).ready" part of my "functions.js" file which is included in every page footer. It's pretty simple. It gets the full current URL of the displayed page and compares it to the full anchor href URL. If they are the same, set anchor (li) parent as active. And do this only if anchor href value is not "#", then the bootstrap will solve it.

$(document).ready(function () {         
    $(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;
                    }
            }
        }); }); });

Solution 4

For single-page sites where the menu items simply jump down to other sections of the page, this simple solution works for me:

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

Solution 5

For those using Codeigniter, add this below your sidebar menu,

<script>
    $(document).ready(function () {
        $(".nav li").removeClass("active");
        var currentUrl = "<?php  echo current_url();  ?>";
        $('a[href="' + currentUrl + '"]').parents('li,ul').addClass('active');
    });
</script>
Share:
108,642
Priyanka
Author by

Priyanka

I'd completed my graduation in B.Sc.(Computer Science) &amp; Completed my post graduation in MCA. From last 6 months i'm working as a .NET software engineer in GS. I'm C# programmer. Right now i'm working on one project named MsgBlaster 4.0 which will be another version of earlier software MsgBlaster 3.0 which is message sending s/w from PC to mobile. It not required any mobile device or SMS modem connectivity with PC. And i like to use Stackoverflow when i stuck with any problem related to my project &amp; it helps me a lot... thanks.

Updated on October 22, 2020

Comments

  • Priyanka
    Priyanka over 3 years

    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.

  • Priyanka
    Priyanka about 11 years
    thanks dakait. It solve my problem. But the thing is that i have to write script on each web pages then its difficult to maintain if my web pages increases. Is there any other way then i can generalize the script and keep in layout or may be by using any css property we an do this ?
  • Dakait
    Dakait about 11 years
    @Priyanka i was in the same situation yesterday i solved the problem using the localStorage which is kind of a workaround too but then i dont have to write the js on each page. the reason is page gets reloaded and from the localStorage i retrieve the clicked li's id and apply class to it. if i find a better solution ill update the answer... P.S you may mark the answer correct since it solved your problem :p
  • bubak
    bubak almost 9 years
    also you can add st like this: $('nav a').parents('li,ul').removeClass('active'); to automatically remove any remaining 'active' if needed
  • PongGod
    PongGod over 6 years
    This isn't working for me so I'm curious to know what I'm doing wrong. My app is using web forms and I put this in the head section of my master page file.
  • kanlukasz
    kanlukasz about 4 years
    Great solution and works nice with dropdowns. I tried thousands other solution but this one is the best
  • user441521
    user441521 about 4 years
    By far the easiest and best solution for this. Just brilliant!