GhostWerx
asked on
jQuery to add class to link list item based on href value
Hi all,
if I have the following code:
<ul>
<li><a href="/some-page">Some Page</a></li>
<li><a href="/another-page">Anoth er Page</a></li>
</ul>
How can I use jQuery to add the class "selected" to the li based on the href="/another-page".
So jQuery scans the list, if the href value equals "/another-page" then add the selected class to the li element so I would then have the following code:
<ul>
<li><a href="/some-page">Some Page</a></li>
<li class="selected"><a href="/another-page">Anoth er Page</a></li>
</ul>
Thanks for your help.
GW.
if I have the following code:
<ul>
<li><a href="/some-page">Some Page</a></li>
<li><a href="/another-page">Anoth
</ul>
How can I use jQuery to add the class "selected" to the li based on the href="/another-page".
So jQuery scans the list, if the href value equals "/another-page" then add the selected class to the li element so I would then have the following code:
<ul>
<li><a href="/some-page">Some Page</a></li>
<li class="selected"><a href="/another-page">Anoth
</ul>
Thanks for your help.
GW.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$("li a[href='/another-page']").parent().addClass("selected")
ASKER
Thanks heaps.
Open in new window