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 & 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.
jQueryso why you taggedangularjs?console.log($('nav li:has(a[href="' + pageUrl + '"])'))insideifcondition and check whether you are getting the DOM therer.fn.init(0) length : 0 prevObject : r.fn.init [document] __proto__ : Object(0)pageUrlmay have the full URL, whereas youatag will only have the relative path