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
  • 5,166
  • 6
  • 21
  • 43
rek
  • 177
  • 6

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
  • 180,066
  • 36
  • 283
  • 292
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
  • 1,797
  • 2
  • 9
  • 23