Reputation: 373
I have a some jQuery code:
jQuery('li:has(.selected)').addClass('test');
and I need to add class to li
if li
has a
with class .selected
.
Upvotes: 1
Views: 1054
Reputation: 10216
Try this:
$(function() {
$('li').has('a.selected').addClass('test');
});
Another easy thing would be to just select the a itself:
$(function() {
$('a.selected').parent('li').addClass('test');
});
Upvotes: 1
Reputation: 1074038
I need to add class to LI if LI has A with class .selected
So the li
will have an a.selected
in it? Then:
jQuery("a.selected").parent("li").addClass("test");
or possibly
jQuery("li > a.selected").parent().addClass("test");
or if you really mean ancestor, not parent:
jQuery("li a.selected").closest('li').addClass("test");
More:
Upvotes: 3