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

Line ending issue with Mercurial or Visual Studio -

java - Jtable duplicate Rows -

java - Run a .jar on Heroku -