Reputation: 717
I want to append if the li says "Thing" but NOT if it says "Things". Is that possible with :contains
?
$("ul li:nth-child(2):contains('Thing')").append("foo");
Upvotes: 2
Views: 138
Reputation: 1827
There is also a :not() selector which helps compressing the code a bit.
So it would be like
$("ul li:nth-child(2):contains('Thing'):not(:contains('Things'))").append("foo");
A small example in the fiddle.
Upvotes: 0
Reputation: 144689
If you want to select the element if that exactly has Thing
text content, you can use filter
method:
$("ul li:nth-child(2)").filter(function(){
return $(this).text().trim() === 'Thing'
}).append("foo");
Upvotes: 3
Reputation: 382130
There's a not function :
$("ul li:nth-child(2):contains('Thing')").not(':contains("Things")').append("foo");
Upvotes: 7