HTML DOM - Events
The HTML DOM allows JavaScript to react to HTML events.
Example
Mouse Over Me
Click Me
React to Events
When an event occurs, JavaScript can be executed, such as when a user clicks an HTML element.
To execute code when a user clicks an element, add JavaScript code to the HTML event attribute:
onclick=
JavaScript
Examples of HTML events:
- When a user clicks the mouse
- When a web page has loaded
- When an image has loaded
- When the mouse moves over an element
- When an input field is changed
- When an HTML form is submitted
- When a user presses a key
In this example, when the user clicks, the
element's content changes:
Example
<h1 onclick="this.innerHTML='Ooops!'">Click the text!
h1
Try it »
In this example, a function is called from the event handler:
Example
<
script
function changetext(id){ id.innerHTML="Ooops!"; }
script
head
<body>
<h1 onclick="changetext(this)">Click the text!
h1
Try it »
HTML Event Attributes
To assign events to HTML elements, you can use event attributes.
Example
Assign an onclick event to a button element:
<button onclick="displayDate()">Click me
button
Try it »
In the example above, when the button is clicked, the function named displayDate is executed.
Assign Events Using the HTML DOM
The HTML DOM allows you to assign events to HTML elements using JavaScript:
Example
Assign an onclick event to a button element:
document
.
getElementById
(
"
myBtn
"
)
.
onclick
=
function
(
)
{
displayDate
(
)
}
;
Try it »
In the example above, the function named displayDate is assigned to the HTML element with id=myButn".
When the button is clicked, the function will be executed.
The onload and onunload Events
The onload and onunload events are triggered when the user enters or leaves the page.
The onload event can be used to check the visitor's browser type and version, so that different versions of a web page can be loaded based on this information.
The onload and onunload events can be used to handle cookies.
Example
<body onload="checkCookies()">Try it »
The onchange Event
The onchange event is commonly used for validation of input fields.
The example below shows how to use onchange. When the user changes the content of an input field, the upperCase() function is called.
Example
<input type="text" id="fname" onchange="upperCase()">Try it »
The onmouseover and onmouseout Events
The onmouseover and onmouseout events can be used to trigger a function when the mouse pointer moves onto or away from an element.
Example
A simple onmouseover-onmouseout example:
Mouse Over Me
Try it »
The onmousedown, onmouseup, and onclick Events
The onmousedown, onmouseup, and onclick events are the entire process of a mouse click. First, when a mouse button is clicked, the onmousedown event is triggered; then, when the mouse button is released, the onmouseup event is triggered; and finally, when the mouse click is completed, the onclick event is triggered.
Example
A simple onmousedown-onmouseup example:
Click Me
Try it »
HTML DOM Event Object Reference Manual
For a complete description and examples of each event, please visit our HTML DOM Reference Manual.