Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  146] [ 4]  / answers: 1 / hits: 28770  / 10 Years ago, sat, january 10, 2015, 12:00:00

I have a simple navigation menu. Home link has a dropdown active class. I need to do this:
When user press for example Article link, jquery has to remove dropdown active class from Home and set this class to selected link. I know that I can use removeClass and addClass , but I need to keep selected link class when page is reload. I heard about cookies and localstorage to set class, but I don't know how. Can someone help me?



   <ul>
<li class=dropdown active><a>Home</a></li>
<li class=dropdown><a>Articles</a></li>
<li class=dropdown><a>About</a></li>
</ul>

More From » jquery

 Answers
8

You can use with jquery in this way:



$(document).ready(function(){
if($(.dropdown a).attr(href)==window.location.href){
$(.dropdown).attr(class,dropdown active);
}
else{
$(.dropdown).attr(class,dropdown);
}
});

[#68254] Thursday, January 8, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
guadalupec

Total Points: 610
Total Questions: 91
Total Answers: 91

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;