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

codeigniter - Fatal error: Call to undefined function lang() in CI Merchant using CardSave -

python - Received unregistered task using Celery with Django -

c# - Delving into the world of XML (Windows Phone) Error I dont understand (The ' ' character, hexadecimal value 0x20, cannot be included in a name.) -