Create handler for Onclick button via Javascript code - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Button

Description

Create handler for Onclick button via Javascript code

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body onload="createButton()">   
      <script>
function createButton(){//from  www  .ja  va2 s  . com
var newButton = document.createElement("button");
newButton.onclick = function() { 
  document.write('test') 
};
var textButton = document.createTextNode("new Node");
newButton.appendChild(textButton);
document.body.appendChild(newButton);
}

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

Related Tutorials