Add button to div and attach click event - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Div

Description

Add button to div and attach click event

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from w  ww  .  ja  va 2  s  .com
var widget = document.getElementById("widget");
widget.innerHTML = '<button id="mahbutton">Clicky</button>';
var w_btn = document.getElementById("mahbutton");
w_btn.addEventListener("click",
    function() {
        console.log("sdf");
    }
);
    });

      </script> 
   </head> 
   <body> 
      <div id="widget"> 
      </div>  
   </body>
</html>

Related Tutorials