2

Is there any dom javascript query which can give if a specific innerText exist in the dom.?

Example:

document.querySelectorAll('section.active div.test')[1].innerText('specific text') !== 0
Sarun UK
  • 6,210
  • 7
  • 23
  • 48
rek
  • 177
  • 7

2 Answers2

1

const hasText = (el, text) => el.textContent.includes(text);

const div = document.querySelectorAll('section.active div.test')[1]
console.log(hasText(div, "specific text"));
<section class="active">
  <div class="test">I am a DIV</div>
  <div class="test">I have a specific text!</div>
</section>

To loop all your elements use NodeList.prototype.forEach()

const hasText = (el, text) => el.textContent.includes(text);

document.querySelectorAll('section.active div.test').forEach(el => {
  console.log(hasText(el, "specific text"));
});
<section class="active">
  <div class="test">I am a DIV</div>
  <div class="test">I have a specific text!</div>
</section>
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1

You can check an element's innertext for some specific text as follows:

if (document.querySelectorAll('section.active div.test')[1].innerText).indexOf('specific text') > -1) {
// some code here
}
Ozgur Sar
  • 2,067
  • 2
  • 11
  • 23