Jquery: Checking to see if div contains text, then action
Yes, I now made think for me. And it works fine!!!
if($("div:contains('CONGRATULATIONS')").length)
{
$('#SignupForm').hide(500);
}
Ayman is right but, you can use it like that as well :
if( $("#field > div.field-item").text().indexOf('someText') >= 0) {
$("#somediv").addClass("thisClass");
}
if( $("#field > div.field-item").text().indexOf('someText') >= 0)
Some browsers will include whitespace, others won't. >=
is appropriate here. Otherwise equality is double equals ==
Your code contains two problems:
- The equality operator in JavaScript is
==
, 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");
}