JavaScript HTML DOM Events

Source: Internet
Author: User
Tags html form

JavascriptHTML DOM Events

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

Instance mouse over Me

Respond to Events

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

To execute code when a user taps an element, add JavaScript code to an HTML event property:

onclick= JavaScript

Examples of HTML events:

    • When the user clicks the mouse
    • When the page is loaded
    • When the image is loaded
    • When the mouse moves over the element
    • When the input field is changed
    • When you submit an HTML form
    • When the user triggers the button

In this case, when the user clicks on the

Example <! DOCTYPE html>
<body>
&LT;H1 onclick= "this.innerhtml= ' ooops! '" > Click Text!</body>
Try it»

This example calls a function from the event handler:

Example <! DOCTYPE html>
<script>
function Changetext (ID)
{
Id.innerhtml= "ooops!";
}
</script>
<body>

</body>
Try it»

HTML Event Properties

If you want to assign an event to an HTML element, you can use the event properties.

Instance

To assign the OnClick event to the button element:

<buttononclick= "displaydate ()" >try it</button>
Try it»

In the example above, the function named Displaydate will be executed when the button is clicked.

Use HTML DOM to assign events

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

Instance

To assign the OnClick event to the button element:

<script>
document.getElementById ("Mybtn").onclick=function () {displaydate ()};
</script>
Try it»

In the example above, a function named Displaydate is assigned to the HTML element of Id=mybutn ".

The JavaScript function will be executed when the button is clicked.

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 browser type and browser version of the visitor and to load the correct version of the Web page based on that information.

The onload and OnUnload events can be used to process cookies.

Example <bodyonload= "checkcookies ()" >
Try it»

onchange Events

OnChange events are often used in conjunction with validation of input fields.

Here is an example of how to use onchange. The uppercase () function is called when the user changes the contents of the input field.

Instance <input type= "text" id= "FName"onchange= "uppercase ()" >
Try it»

onmouseover and onmouseout Events

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

Instance

A simple example of onmouseover-onmouseout:

Mouse over Me
Try it»

OnMouseDown, onmouseup, and onclick events

OnMouseDown, onmouseup, and onclick form all parts of the mouse click event. First, when the mouse button is clicked, the OnMouseDown event is triggered, and when the mouse button is released, the OnMouseUp event is triggered, and the onclick event is triggered when the mouse click is completed.

Instance

A simple example of onmousedown-onmouseup:

Thank

More examples

OnMouseDown and OnMouseUp
Replace an image when the user presses the mouse button.

OnLoad
When the page finishes loading, a prompt box is displayed.

onfocus
Changes the background color of the input field when it gets focus.

Mouse events
When the pointer moves over the element, it changes its color, and when the pointer moves out of the text, it changes its color again.

JavaScript HTML DOM Events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.