Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript HTML DOM event

HTML DOM JavaScript, HTML has the ability to react to events.

Examples

Mouse Over Me
Click Me


React to events

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

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

onclick= JavaScript

HTML Event examples:

  • 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 key

In this example, when a user on the <h1> element when clicked, will change its contents:

Examples

<! DOCTYPE html>
<Html>
<Body>
<H1 onclick = "this.innerHTML = 'Ooops!'"> Click on the text! </ H1>
</ Body>
</ Html>

try it"

In this case to call a function from the event handler:

Examples

<! DOCTYPE html>
<Html>
<Head>
<Script>
function changetext (id)
{
id.innerHTML = "Ooops!";
}
</ Script>
</ Head>
<Body>
<H1 onclick = "changetext (this)"> click on the text! </ H1>
</ Body>
</ Html>

try it"


HTML Event Attributes

To assign events to HTML elements, you can use the event attributes.

Examples

Assigned to the button element onclick event:

<button onclick = "displayDate () "> Click here </ button>

try it"

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


Use the HTML DOM to assign an event

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

Examples

Assigned to the button element onclick event:

<Script>
. document.getElementById ( "myBtn") onclick = function () {displayDate ()};
</ Script>

try it"

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

Click the button when the Javascript function will be executed.


onload and onunload event

onload and onunload event is triggered when the user enters or leaves the page.

onload event can be used browser type and version of browser the visitor is detected, and based on this information to load the correct version of the page.

onload and onunload event can be used to handle cookie.

Examples

<body onload = "checkCookies () ">

try it"


onchange event

onchange event often combined input field validation to use.

Here is an example of how to use the onchange. When the user changes the contents of the input field, calls upperCase () function.

Examples

<input type = "text" id = "fname" onchange = "upperCase ()">

try it"


onmouseover and onmouseout events

onmouseover and onmouseout events can be used to trigger functions when the user's mouse moves to the top of the HTML element or elements removed.

Examples

A simple onmouseover-onmouseout instance:

Mouse Over Me

try it"


onmousedown, onmouseup and onclick event

onmousedown, onmouseup and onclick constitute all parts of the mouse click event. First, when you click the mouse button, the trigger onmousedown event when the mouse button is released, it will trigger onmouseup event, and finally, when the completion of a mouse click, it will trigger the onclick event.

Examples

A simple onmousedown-onmouseup instance:

Thank You


More examples

onmousedown and onmouseup
When the user presses the mouse button, the replacement of an image.

onload
When the page is finished loading, display a message box.

onfocus
When the input field has the focus, change the background color.

Mouse events
When the pointer moves over the element, change its color; when the pointer moves out of the text, its color will change again.