Author: MDBootstrap
Events are "things" that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript can "react" on these events.
An 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
Often, when events happen, 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.
In the following example, we use onclick
event to run myFunction
when the button with an ID button-test
is clicked
<!-- Button with an ID "button-test" -->
<button id="button-test" type="button">Click me!</button>
<!-- Empty paragraph with an ID "example-1" -->
<p id="example-1"></p>
<script>
// That instruction says "If you click button with ID 'button-test' run myFunction"
document.getElementById("button-test").onclick = function () {
myFunction()
};
// That instruction says "Put this text into a paragraph with an ID "example-1"
function myFunction() {
document.getElementById("example-1").innerHTML = "WOW, YOU CLICKED ME!";
}
</script>
Live preview
Example 2
<!-- Button with an ID "button-test-2" -->
<button id="button-test-2" type="button">Click me!</button>
<!-- Empty paragraph with an ID "example-2" -->
<p id="example-2"></p>
<script>
// That instructions says "If you click button with ID 'button-test' put a current date in the paragraph with an ID "example-2"
document.getElementById("button-test-2").onclick = function () {
document.getElementById("example-2").innerHTML = Date();
};
</script>
Live preview
Common HTML Events
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 |
What are events useful for?
Event can be used to handle, and verify, user input, user actions, and browser actions:
- Things that should be done every time a page loads
- Things that should be done when the page is closed
- Action that should be performed when a user clicks a button
- Content that should be verified when a user inputs data
- And more ...
Note: You will learn a lot more about events in the next tutorials.
Exercises - test your knowledge
Exercise 1
Create a button that will alert "hello" when someone click on it.
Show answer
<button id="button-ex-1" type="button">Click me!</button>
<script>
document.getElementById("button-ex-1").onclick = function () {
alert("hello");
};
</script>
Exercise 2
Create a button that will execute function "exampleFunction" when someone click on it
Show answer
<button id="button-ex-2" type="button">Click me!</button>
<script>
document.getElementById("button-ex-2").onclick = function () {
exampleFunction();
};
</script>
Exercise 3
Create a div element that will change a background color when someone moves the mouse over it.
Show answer
<div id="example-3">Example div</div>
<script>
document.getElementById("example-3").onmouseover = function () {
document.getElementById("example-3").style.backgroundColor = 'red';
};
</script>
Previous lesson Next lesson
Spread the word: