Add event to element when DOM is loaded - Javascript DOM

Javascript examples for DOM:Event

Description

Add event to element when DOM is loaded

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <div id="TABLE"> 
      </div> 
      <script>
    window.onload = function () {
      var table = window.document.getElementById("TABLE");
      var button = document.createElement("BUTTON");
      button.id = "BUTTON_ID";
      button.innerHTML = "Button";
      button.onclick = function() {
          console.log('clicked');
      };//  ww w.  j  a va 2  s.  co m
      table.appendChild(button);
   }

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

Related Tutorials