You can assign events to HTML elements by using event attributes.
Attach an onclick event to a button element:
<button onclick=”displayDate()”>Try it</button> |
In the example above, a function called displayDate will be triggered when the button is clicked.
The HTML DOM enables you to assign events to HTML elements through JavaScript.
Attach an onclick event to a button element:
<script> document.getElementById(“myBtn”).onclick = displayDate; </script> |
In the example above, a function named displayDate is assigned to the HTML element with id=”myBtn”. The function will be executed when the button is clicked.