Reputation: 3
I want add class to <li>
element with <a href="http://localhost/hello/5">
element.
I try, but it don't work:
$("<li><a>[href|='http://localhost/hello/5']").addClass( "active" );
Upvotes: 0
Views: 90
Reputation: 12069
To add to the parent <li>
:
$("[href='http://localhost/hello/5']").parent("li").addClass( "active" );
Upvotes: 2
Reputation: 43790
Remove the <
and >
from your selectors. That creates elements rather than selecting them. Since you want to add the class to the li of the a with the href, you need to select the link and then move up the DOM to the li. So that you are adding the class to the right element. It should be:
$("a[href='http://localhost/hello/5']").parent("li").addClass( "active" );
Upvotes: 1