+ 3
How to attach onclick method to input of button type?
Where is my mistake? <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <input id="button1" onclick="alert('Hello World!')" value="Click Me!"> <script> var el = document.getElementById("button1"); el.type = "button"; el.value = "Click me!"; el.onclick="alert('Hello World!')"; </script> </body> </html>
4 Respostas
+ 22
el.onclick = function() {alert("spam");};
+ 3
el.addEventListener("click", function (){ alert('Hello World'); })
+ 3
Thank you very much!
Actually both of them work!
I like ValentinHacker's one as its logic is closest to my question. And thank you, Luca and Yaroslav, I will keep in mind Yaroslav's method for adding multiple functions.
+ 2
Yaroslav's method allows you to bind multiple functions to an event.