Diya
Diya

Reputation: 1

How to read text "LE 88" from the following HTML using Javascript

How to read text "LE 88" from the following HTML using Javascript. the text LE is not constant, it keeps on changing. It neither contains ID's, nor class names.

 <body>
        <div id="Record">
            <div>
                <div>Grade A </div>
            </div>
            <div>19-04-2022
            </div>
            <div>
                <div>Subject H1
                </div>
                <div>
                    <div>LE 88
                    </div>
                </div>
            </div>
            <div>
            </div>
        </div>    
    </body>

Upvotes: 0

Views: 31

Answers (1)

brk
brk

Reputation: 50291

You can iterate the dom div and check for the children. In this case the div with text does not have any child. So use children and check for the textContent. If the matching text is found do the required operation

document.querySelectorAll('div').forEach((curr) => {
  const child = curr.children;
  if (child.length === 0 && curr.textContent.trim() === 'Marks 88') {
    curr.textContent = 'Marks 88 changed'
  }

})
<div>
  <div>
    <div>Grade A </div>
  </div>
  <div>19-04-2022</div>
  <div>
    <div>Subject H1</div>
    <div>
      <div>Marks 88</div>
    </div>
  </div>
  <div>
  </div>
</div>

Upvotes: 0

Related Questions