This example demonstrates how to execute a function when a user clicks on a <button> element:
This example uses the addEventListener()
method to execute a function when a user clicks on a button.
<!DOCTYPE html> <html> <body> <button id="myBtn">Test</button> <p id="demo"> <script> document.getElementById("myBtn").addEventListener("click", myFunction); function myFunction() {//w w w . ja v a 2 s . c o m document.getElementById("demo").innerHTML = "Hello World"; } </script> </body> </html>