Handling Events
There are several ways to handle events, including the following.
Write the code directly into the HTML. <button onclick="alert('This is an alert');">
Click Me
</button>
The code inside the quotes of the onclick event handler will be executed once the button is clicked.
Alternatively, handle the event in JavaScript by assigning the function to be executed to the element's corresponding event:
element.onclick = function() { //event handler code };
Here's a simple example:
<html> <body> <button id="demo"> Click me </button> <script> var elem = document.getElementById("demo"); elem.onclick = function(){ alert("Clicked!"); }; </script> </body> </html>