0

When i use ID in my elements and write my code docuemnt.getElementById using function with onclick property it works but when i use class in my elements then write document.getElementsByClassName it does not work although it does not give an error but not given a answer, kindly help and guid Here is a code below

document.getElementsByClassName('d1').onclick = function(){
        console.log("Hello World")
}

& 
  
document.getElementById('d1').onclick = function(){
        console.log("Hello World")
}

<div class="d1">1  <!-- the topmost -->
    <div class="d2">2
        <div class="d3">3 <!-- the innermost -->
        </div> 
    </div>
 </div>
Haseeb Jumani
  • 65
  • 1
  • 6

0 Answers0