jquery: check if string exist in element, return boolean

I used the same way of the buddy above...:

if($('#div_searched').text().indexOf('bla') != -1)

This will return true when it found the word bla on div_searched


You can use the :contains() selector and check the .length to see if it matched anything, like this:

return $("#elementId:contains('some text')").length > 0;

let myNode = document.getElementById("elementId")
const Value = myNode.textContent || myNode.innerText
const txtExist = Boolean(Value.replace(/\s/g, ""));

Note: innerText does work in IE, also textContent is not supported in IE8 and older


var isContains = $('#elementId').text().indexOf('some text') > -1;

Tags:

Jquery