Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a Div or it's children contain a specific string/substring?

In JQuery/JavaScript, how can I find out if a Div contains a specific string/substring in one of it's child elements? I've looked into both contains and indexOf but couldn't find any examples of my specific case where I need to commit a different action if a unique string/substring of that string is found.

Pseudo Code:

if (div contains (itself or child nodes) "this string/part of this string")
then do something
else 
then do something else 

1 Answers

You can use text() of div to get all the text of the current div and its childs,

if('#divId').text().indexOf("this string/part of this string") != -1)
{

}
else
{

}
like image 56
Adil Avatar answered Jan 01 '26 15:01

Adil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!