Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  149] [ 7]  / answers: 1 / hits: 5806  / 10 Years ago, thu, june 12, 2014, 12:00:00

Im having an issue getting the state of an anchor tag to be active. Say I am on the home page, I want the home navigation link to have an active state so it has a different color. Here is what im doing at the moment. I am referencing jquery. I have stubbed in alerts to make sure my script is getting hit. Here is what I last tried.



<div class=grid__unit one-whole>
<nav role=navigation>
<ul id=nav--primary class=navbar navbar--menu>
<li><a class=navitem href=#>Link</a></li>
<li><a class=navitem href=http://www.google.com>Google</a></li>
<li><a class=navitem id=myNavButton href=myLink>myTitle</a></li>
</ul>
</nav>




with css



a:hover, a:active {
color: #b74800;
}


and script



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


EDIT 1:



I have tried adding an a.active tag to the css and script as follows, with no success.



<script>
$(document).ready(function () {
var path = location.pathname.length ? location.pathname : window.location.href;
$('.active').removeClass('active');
$(#nav--primary).find('a[href*=' + path + ']').addClass(active);
});
</script>


with css



a:hover, a:active, a.active {
color: #b74800;
}


EDIT 2 --SOLVED


More From » jquery

 Answers
4

To get the desired functionality, I ended up adding an ID to the parent list item and on document ready, I added a class to that list item. In css i added another class for this.



#nav--primary .is--selected .navitem,
#nav--primary .is--selected .navitem:hover,
#nav--primary .is--selected .navitem:focus {
background: #colorhere; /* primary nav current page background color */
color: #colorhere /* primary nav current page text color */
}


with script



<script>
$(document).ready(function () {
$(#myLiID).addClass(is--selected);
});
</script>

[#44625] Wednesday, June 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
;