Javascript - check if div contains a word?

user1104092 picture user1104092 · Mar 9, 2012 · Viewed 81.3k times · Source

How can I check if a div contains a certain word?

var divs= document.getElementsByTagName('div');
for (var i = 0, len = divs.length; i < len; ++i) {

    if (divs[i].text = '*word*'){
    //do somthing
}
}

doesn't work.

Answer

Igor Azevedo picture Igor Azevedo · Mar 9, 2012

use the indexOf function

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