user1729506
user1729506

Reputation: 975

jQuery, selecting element that contains a specified string while not containing other

I am using :contains() to find strings in a large page of text content, and all of the repeating blocks will contain strings unique to those blocks, such as a student ID. I want to combine :contains() with :not(:contains()).

The logic is as follows: If string contains 1234, but the word "absent" is not present, append award statement

My goal is to combine the two selectors for a contains-but-doesn't-contain effect.

Upvotes: 0

Views: 458

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You can use filter here:

$(selector).contains("1234").filter(function () {
  return $(this).not(":contains("absent")");
});

Or in simple way:

$('selector:contains(1234):not(:contains(absent))')

Upvotes: 2

Related Questions