JavaScript’s interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event.

HTML events are “things” that happen to HTML elements. When JavaScript is used in HTML pages, JavaScript can “react” on these events.
HTML Events
An HTML event can be something the browser does or something a user does.
Here are some examples of HTML events:
- An HTML web page has finished loading
- An HTML input field was changed
- An HTML button was clicked
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
<element event="some JavaScript">
In the following example, an onclick attribute (with code), is added to a <button> element:
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('example').innerHTML=Date()">The time is?</button>
<p id="example"></p>
</body>
</html>
Output:
In the next example, the code changes the content of its own element (using this.innerHTML):
<!DOCTYPE html>
<html>
<body>
<button onclick="this.innerHTML=Date()">The time is?</button>
</body>
</html>
Output:
Common HTML Events
Event | Description |
onchange | An HTML element has been changed |
onclick | The user clicks an HTML element |
onmouseover | The user moves the mouse over an HTML element |
onmouseout | The user moves the mouse away from an HTML element |
onkeydown | The user pushes a keyboard key |
onload | The browser has finished loading the page |
Enjoy coding!
Hey, here’s something that might interest you: