Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - check if div contains a word?

People also ask

How do you check if an element contains a string?

The includes() method performs a case-sensitive search and checks if the provided string is contained in the string it was called on.

What is innerText and Innerhtml?

innerText returns all text contained by an element and all its child elements. innerHtml returns all text, including html tags, that is contained by an element.


use the indexOf function

if(divs[i].innerHTML.indexOf("word") !== -1) {
    // something
}

Use includes():

node.textContent.includes('Some text');

if (document.getElementById('divId').innerHTML.indexOf("word") != -1) { }

Try the String.indexOf() function: if (divs[i].text.indexOf('word') != -1) {


You have to use a comparison operator not assign a variable.

if (divs[i].text == '*word*'){

I would recommend to use indexOf.

if (divs[i].text.indexOf('*word*') != -1){

In addition to what others said about using .indexOf() function, I'd like to say .text is not a div node property. User .innerHTML

if (divs[i].innerHTML.indexOf('word') > -1){}