I\'m trying to check in jQuery if a div contains some text, and then add a class if it does.
So I wrote something like this:
if( $(\"#field >
Your code contains two problems:
==
, not =
.jQuery.text()
joins all text nodes of matched elements into a single string. If you have two successive elements, of which the first contains 'some'
and the second contains 'Text'
, then your code will incorrectly think that there exists an element that contains 'someText'
.I suggest the following instead:
if ($('#field > div.field-item:contains("someText")').length > 0) {
$("#somediv").addClass("thisClass");
}