0

I am trying to add active class based on url to show that which li is clicked.

pageUrl = location.pathname;
$('#sidebar-left nav li.active').removeClass("active");
if (pageUrl) {
  $('nav li:has(a[href="' + pageUrl + '"])').addClass("active");
}

this is what I have tried in init() method.
I am getting the pageUrl correctly.But am unable to add active class for clicked li.

html:

<div id="sidebar-left" class="col-xs-2 col-sm-2">
            <ul class="nav main-menu">
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="/employee/Home" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-home"></i>
                        <span class="hidden-xs ng-binding">Home</span>
                    </a>                       
                </li>
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="/documents/doc_details" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-file-text"></i>
                        <span class="hidden-xs ng-binding">Documents</span>
                    </a>                       
                </li>
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="#" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-money"></i>
                        <span class="hidden-xs ng-binding">Pay &amp; Benifits</span>
                    </a>
                    <ul class="dropdown-menu">
                       <li ng-repeat="child in parent.children" class="ng-scope">
                            <a href="/pay/paymanagement" ng-click="tabName(child.name)" class="ng-binding">slips</a>
                        </li>
                    </ul>
                </li>                    
            </ul>
        </div>

Note: I can add active class using url only. Because each li has different pages so whenever I click the li, page will be refreshed.so I cant css/onclick/.click here.

Please give me some suggestion.

10
  • This is jQuery so why you tagged angularjs ? Commented Jan 22, 2018 at 6:32
  • give this console.log($('nav li:has(a[href="' + pageUrl + '"])')) inside if condition and check whether you are getting the DOM there Commented Jan 22, 2018 at 6:33
  • I thought there may be some other way in angularjs to achieve this. Commented Jan 22, 2018 at 6:33
  • @AswinRamesh this is what I got.. r.fn.init(0) length : 0 prevObject : r.fn.init [document] __proto__ : Object(0) Commented Jan 22, 2018 at 6:35
  • @laz this means the element you are searching for doesn't exist in the page, it might be because the pageUrl may have the full URL, whereas you a tag will only have the relative path Commented Jan 22, 2018 at 6:37

3 Answers 3

2

When you set an href url it automatically turns to full URL in JavaScript. So pathname in your code won't work. Instead lets use the filter() method from jquery.

$(function() {
    $('#sidebar-left nav li.active').removeClass("active");
    $('#sidebar-left nav li a').filter(function(){
        return this.href.indexOf(window.location.href) !== -1;
    }).parent().addClass("active");
});
Sign up to request clarification or add additional context in comments.

3 Comments

let me try:-):-)
It should work. I checked lbu code it is working for me.
I think am adding my code in wrong place.page is getting refreshed after clicking the menu.so added active class may be hide right?
1

you missed the dot . in front of nav class

It should be like

$('#sidebar-left .nav li.active').removeClass("active");
if (pageUrl) {
  $('.nav li:has(a[href="' + pageUrl + '"])').addClass("active");
}

it should work if your location returns correctly, a minimal code you can find it here

NB: I have hardcoded the URL

$(document).ready(function() {
  pageUrl = '/employee/Home'
  $('#sidebar-left .nav li.active').removeClass("active");
  if (pageUrl) {
     $('.nav li:has(a[href="' + pageUrl + '"])').addClass("active");
  }
})
.active {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sidebar-left" class="col-xs-2 col-sm-2">
            <ul class="nav main-menu">
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="/employee/Home" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-home"></i>
                        <span class="hidden-xs ng-binding">Home</span>
                    </a>                       
                </li>
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="/documents/doc_details" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-file-text"></i>
                        <span class="hidden-xs ng-binding">Documents</span>
                    </a>                       
                </li>
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="#" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-money"></i>
                        <span class="hidden-xs ng-binding">Pay &amp; Benifits</span>
                    </a>
                    <ul class="dropdown-menu">
                       <li ng-repeat="child in parent.children" class="ng-scope">
                            <a href="/pay/paymanagement" ng-click="tabName(child.name)" class="ng-binding">slips</a>
                        </li>
                    </ul>
                </li>                    
            </ul>
        </div>

15 Comments

still am facing the same probem :-(
@laz can you tell what it gives you now console.log($('.nav li:has(a[href="' + pageUrl + '"])')) inside the if
it gives the same thing..which I posted initially :-(
I think am adding my code in wrong place.page is getting refreshed after clicking the menu.so added active class may be hide right?
so if in that case, can you add it in document ready, like in my snippet
|
1

Iam using this script for adding class active to the a element when the url matched with current page url.

$(function(){
       pageUrl = location.pathname;
       $('nav ul li a').each(function () {
            link = $(this);
            if (link.attr("href") == pageUrl) {
                link.addClass("active");
            } 
       });
   });

The element structure look like below:

<nav>
    <ul>
        <li>
            <a href="/test/a/">Testing</a>
            <a href="/test/b/">Testing</a>
        </li>
    </ul>
</nav>

For your case, you could use this code:

$(function(){
       pageUrl = location.pathname;
       $('#sidebar-left ul li a').each(function () {
            link = $(this);
            if (link.attr("href") == pageUrl) {
                link.parent().addClass("active");
            } 
       });
   });

3 Comments

I think am adding my code in wrong place.page is getting refreshed after clicking the menu.so added active class may be hide right?
If your code inside $(function(){//yourcode}) or $(document).ready(function(){//your code});is not problem with refreshed page. Because the code execute after page has loaded or on document ready.
do not add the code inside onClick event. but put it on somewhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.