javascript - Onclick button doesn't function -


i try create button when page load.

<!doctype html> <html> <head> <script> function createbutton(){ var newbutton = document.createelement("button"); newbutton.onclick="document.write('tasto premuto')"; var textbutton = document.createtextnode("premi qui"); newbutton.appendchild(textbutton); document.body.appendchild(newbutton); } </script> </head> <body onload="createbutton()">  </body> </html> 

the button created succesfully, function have associated onclick event doesn't work. ideas?

onclick expects function, not string:

newbutton.onclick = function() { document.write('tasto premuto') }; 

please see jsfiddle

of course, should aware document.write() clears dom of current content, rather appending string existing content.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -