Table of Contents
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:
Often, when events occur, you may want to do something.
JavaScript lets you execute code when events are detected.
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>HTML Events</h4>
<p>Click the button to display the date.</p>
<button onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>
<p id="demo"></p>
</body>
</html>

Result View Example
You can call a JavaScript function when an event occurs, such as when a user clicks on an element:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Calling a JavaScript Function</h4>
<p id="demo">Click the button to call a function.</p>
<button onclick="myFunction()">Click me</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Function called!";
}
</script>
</body>
</html>

Result View Example
Here is a list of some 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 |
The addEventListener() method attaches an event handler to an element without overwriting existing event handlers.
You can add many event handlers to one element. You can also add many event handlers of the same type to one element.
You can add event listeners to any DOM object not only HTML elements. i.e. the window object.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Using an Event Listener</h4>
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
document.getElementById("demo").innerHTML = "Button clicked using addEventListener!";
});
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.