Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
HTML Events
Every element on an HTML page has events which can trigger a JavaScript.
For example, we can use the onClick event of a button element to indicate that a
function will run when a user clicks on the button. We define the events in the
HTML tags.
Examples of events:
A mouse click
A web page or an image loading
Mousing over a hot spot on the web page
Selecting an input field in an HTML form
Submitting an HTML form
A keystroke
In the following example, the content of the h1 element will change when a user
clicks on it:
Example
<html>
<body>
<h1 onclick="this.innerHTML='Ooops!'">Click on this text</h1>
</body>
</html>
Triggering of an Event
addEventListener Method
The first parameter is the type of the event (like "click" or "mousedown").
The second parameter is the function we want to call when the event occurs.
The third parameter is a boolean value specifying whether to use event bubbling or event
capturing. This parameter is optional.
Note that you don't use the "on" prefix for the event; use "click" instead of
"onclick".