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

HTML DOM Events

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:

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.