Skip to content
Advertisement

How to get certain element (using JavaScript) from HTML with similar classes?

How can I get one certain element from HTML using JavaScript. I have this code.

<div class="Content"> 

 <div class="First Block">
  <div class="num">Text From First Block</div>
 </div>

 <div class="Second Block">
  <div class="num">Text From Second Block</div>
 </div>

 <div class="Third Block">
  <div class="num">Text Third Second Block</div>
 </div>

</div>

I want to write “Text From Second Block”. If I write this code

console.log(document.querySelector('.num').innerText);

I will get “Text From First Block”

Advertisement

Answer

instead of using queryselector you can use getElementsByClassName() and instead of enter the index number of your tag

console.log(document.getElementsByClassName("Block")[i].innerHTML)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement