Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript HTML DOM Events

JavaScript HTML DOM Events


The HTML DOM gives JavaScript the ability to react to HTML events.

Example

Mouse Over Me

Click Me


Reacting to Events

We can execute JavaScript when an event occurs, such as when the user clicks on an HTML element.

To execute code when the user clicks an element, add JavaScript code to an HTML event attribute:

onclick=

JavaScript

Examples of HTML events:

In this example, when the user clicks on

element, its content will change:

Example

Click the text!

Try it »

This example calls a function from an event handler:

Example

Click the text!

Try it »


HTML Event Attributes

To assign an event to an HTML element, you can use event attributes.

Example

Assign an onclick event to a button element:

onclick

="

displayDate()

">Click Here

Try it »

In the example above, the function named displayDate will execute when the button is clicked.


Assign Events Using the HTML DOM

The HTML DOM allows you to use JavaScript to assign events to HTML elements:

Example

Assign an onclick event to a button element:

Try it »

In the example above, the function named displayDate is assigned to the HTML element with id="myBtn".

When the button is clicked, the JavaScript 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 detect the visitor's browser type and version, and based on this information, load the correct version of the web page.

The onload and onunload events can be used to handle cookies.

Example

onload

="checkCookies()">

Try it »


The onchange Event

The onchange event is often used together with validation of input fields.

The following is an example of how to use onchange. When the user changes the content of the input field, the upperCase() function is called.

Example

onchange

="upperCase()">

Try it »


The onmouseover and onmouseout Events

The onmouseover and onmouseout events can be used to trigger functions when the user's mouse moves over or out of an HTML element.

Example

A simple onmouseover-onmouseout example:

Mouse Over Me

Try it »


The onmousedown, onmouseup, and onclick Events

onmousedown, onmouseup, and onclick make up all the parts of a mouse click event. First, when the mouse button is pressed, the onmousedown event is triggered; when the mouse button is released, the onmouseup event is triggered; finally, when the mouse click is completed, the onclick event is triggered.

Example

A simple onmousedown-onmouseup example:

Thank You


More examples

onmousedown and onmouseup
Change an image when the user presses the mouse button.

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

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

Mouse Events
Change the color when the pointer moves over the element; when the pointer moves out of the text, change the color again.