JavaScript HTML DOM Events

Toggle navigation
TUTORIAL HOME
JavaScript HTML DOM Events
❮ Previous Next ❯
HTML DOM allows JavaScript to react to HTML events:

Mouse Over Me
Click Me
Reacting to Events
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.

To execute code when a user clicks on an element, add JavaScript code to an 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 been loaded
When the mouse moves over an element
When an input field is changed
When an HTML form is submitted
When a user strokes a key
In this example, the content of the

element is changed when a user clicks on it:

Example

Click on this text!

»
In this example, a function is called from the event handler:

Example

Click on this text!

function changeText(id) {
    id.innerHTML = “Ooops!”;
}

»
HTML Event Attributes
To assign events to HTML elements you can use event attributes.

Example
Assign an onclick event to a button element:

Try it
»
In the example above, a function named displayDate will be executed when the button is clicked.

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 = displayDate;

»
In the example above, a function named displayDate is assigned to an HTML element with the id=”myBtn”.

The function will be executed when the button is clicked.

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 browser version, and load the proper version of the web page based on the information.

The onload and onunload events can be used to deal with cookies.

Example

»
The onchange Event
The onchange event is often used in combination with validation of input fields.

Below is an example of how to use the onchange. The upperCase() function will be called when a user changes the content of an input field.

Example

»
The onmouseover and onmouseout Events
The onmouseover and onmouseout events can be used to trigger a function when the user mouses over, or out of, an HTML element:

Mouse Over Me
»

The onmousedown, onmouseup and onclick Events
The onmousedown, onmouseup, and onclick events are all parts 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, finally, when the mouse-click is completed, the onclick event is triggered.

Click Me
»

More Examples
onmousedown and onmouseup
Change an image when a user holds down the mouse button.

onload
Display an alert box when the page has finished loading.

onfocus
Change the background-color of an input field when it gets focus.

Mouse Events
Change the color of an element when the cursor moves over it.

HTML DOM Event Object Reference
For a list of all HTML DOM events, look at our complete HTML DOM Event Object Reference.

Test Yourself with Exercises!
Exercise 1 »   Exercise 2 »   Exercise 3 »

❮ Previous Next ❯
COLOR PICKER

LEARN MORE

Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
JS Animations
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Loaders
Tooltips
Slideshow
Filter List
Sort List
SHARE

CERTIFICATES

HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.

Read More »
REPORT ERROR
PRINT PAGE
FORUM
ABOUT
×
Your Suggestion:
Your E-mail: 
Page address: 
Description: 
Submit

×
Thank You For Helping Us!
Your message has been sent to Omegas.

Top 10 Tutorials

HTML Tutorial
CSS Tutorial
JavaScript Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
SQL Tutorial
PHP Tutorial
jQuery Tutorial
Angular Tutorial
XML Tutorial
Top 10 References

HTML Reference
CSS Reference
JavaScript Reference
W3.CSS Reference
Browser Statistics
PHP Reference
HTML Colors
HTML Character Sets
jQuery Reference
AngularJS Reference
Top 10 Examples

HTML Examples
CSS Examples
JavaScript Examples
W3.CSS Examples
HTML DOM Examples
PHP Examples
ASP Examples
jQuery Examples
Angular Examples
XML Examples
Web Certificates

HTML Certificate
CSS Certificate
JavaScript Certificate
jQuery Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate
Omegas is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2017 by Refsnes Data. All Rights Reserved.
Powered by W3.CSS.

Leave a comment