d
Amit DhamuSoftware Engineer
 

Find a HTML Element With Text

1 minute read 00000 views

Say you have some HTML markup with the following and want to retrieve the Find Me text, find will let us do this.

<div class="container">
  <span> </span>
  <span></span>
  <span></span>
  <span> Find Me</span>
  <span> </span>
</div>
Array.from(document.querySelectorAll('.container span'))
  .find(f => f.textContent.trim() !== '')
  .textContent.trim()