user3486616
user3486616

Reputation: 3

Add class to HTML element

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

Answers (2)

Tim Withers
Tim Withers

Reputation: 12069

To add to the parent <li>:

$("[href='http://localhost/hello/5']").parent("li").addClass( "active" );

Upvotes: 2

Schleis
Schleis

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

Related Questions