May 06, 2021 JavaScript
Events are behaviors that can be detected by JavaScript.
HTML events are things that happen on HTML elements.
JavaScript can trigger these events when using JavaScript on HTML pages.
HTML events can be browser behavior or user behavior.
Each element in an HTML page can produce events that can trigger JavaScript functions.
Here's an example of an HTML event:
Usually, you can do something when something happens.
JavaScript can execute some code when an event is triggered.
Event properties can be added to HTML elements, and HTML elements can be added using JavaScript code.
Single quotes:
Double quotes:
In the following example, the onclick property (with code) is added to the button element:
In the above example, the JavaScript code modifies the contents of the id"demo" element.
In the next instance, the code modifies the contents of its own element (using this .innerHTML):
JavaScript code is usually a few lines of code. It is more common to call through event properties: |
Here's a list of some common HTML events:
Event | Describe |
---|---|
onchange | HTML elements change |
onclick | The user clicks on the 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 presses the keyboard button |
onload | The browser has finished loading the page |
More event list: JavaScript Reference Manual - HTML DOM Events.
Events can be used to process form validation, user input, user behavior, and browser actions:
There are several ways to execute JavaScript event code:
You'll learn more about events and event handlers in the HTML DOM section. |