Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

HTML Event Attributes

You can assign events to HTML elements by using event attributes.

Example

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.

Assign Events Using the HTML DOM

The HTML DOM enables you to assign events to HTML elements through JavaScript.

Example

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.