Get element by class name via getElementsByClassName() method - Javascript DOM

Javascript examples for DOM:Element getElementsByClassName

Description

Get element by class name via getElementsByClassName() method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width"> 
   </head> 
   <body> 
      <button class="buttonClassName">click me</button> 
      <script>
function myFunc(){//w w  w  . j av a  2 s .  c  o m
  console.log("Got it");
}
document.getElementsByClassName('buttonClassName')[0].onclick = myFunc;

      </script>  
   </body>
</html>

Related Tutorials