Florin Simion
Florin Simion

Reputation: 458

jQuery target only li tags where a element have a certain class

Is there a way to target only li elements where the a tag has a specific class?

for example:

<li> <a href="#" class="mobile-only">link</a> </li>
<li> <a href="#" class="other-class">link</a> </li>

Can I just target the li where a has class "mobile-only"?

Upvotes: 0

Views: 188

Answers (3)

Arpit
Arpit

Reputation: 126

$('a.mobile-only').parent("li")

Upvotes: -1

gurvinder372
gurvinder372

Reputation: 68413

you can access the parent of anchor tag with class mobile-only

$( "a.mobile-only" ).parent();

or you can use parent pseudo selector, but there are performance concerns

li a:parent { background: none; }

js Code $( "li a:parent" )

also the has selector

li:has(a.mobile-only) { background: none; }

js Code $( "li:has(a.mobile-only)" )

Upvotes: 6

Sarhanis
Sarhanis

Reputation: 1587

This is very easy to do with jQuery.

Just do:

$("a.mobile-only").parents("li")

Upvotes: 0

Related Questions