I´m pretty new with Jquery, so I´m not really sure what functions I should be using for my case.
I have a list of links, like so:
<ul class="topmenu">
<li class="selected"><a href='index.html'>Hem</a></li>
<li class=""><a href='services.html'>Tjänster </a></li>
<li class=""><a href='about.html'>Om oss </a></li>
<li class=""><a href='vaccancies.html'>Lediga jobb </a></li>
<li class=""><a href='contact.html'>Kontakta oss </a></li>
</ul>
This is part of my footer, and I´m including this footer in all my pages using jquery load:
$('#footer_include').load('footer.html');
Now, my issue is that I want to be able to dynamically set the class “selected” to my list items, based on what page the user is currently on. So if the user is on vaccancies.html, I want the class for that list item to be set to “selected”. So I´m guessing something like
$(document).ready(function () {
...
$(this).addClass('selected');
...
});
But as you can see, my code is incomplete… Can someone help me finish it? I guess I might need to add IDs to the individual list items aswell?
Cheers!
Add custom data attributes
JQuery:
Example: http://jsfiddle.net/AlienWebguy/CstXK/
If you wanted to use PHP to set the class, you could do this:
Of course that PHP could be cleaned up with an array iteration and a substr, etc., but you get the idea.