nathanbweb
nathanbweb

Reputation: 717

jquery contains() syntax for 'not'?

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

Answers (3)

tombul
tombul

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

Ram
Ram

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

Denys Séguret
Denys Séguret

Reputation: 382130

There's a not function :

$("ul li:nth-child(2):contains('Thing')").not(':contains("Things")').append("foo");

Upvotes: 7

Related Questions