The blog provides the details on the Event Handling in JavaScript. The Front End application allows user to perform action on the available data like user clicks on a button, user enters the data, user press a particular key, user moves the mouse, user closes the window, etc..
The action performed by the user when the state of the object is changed, known as Event. The JavaScript captures these events and allows the execution. This process is known as Event Handling.
JavaScript Libraries like React.js , Vue.js, Angular.js provides the Event and Event Handling as these libraries uses JavaScript.
The generated HTML element contains the set of events which can trigger JavaScript Code.
JavaScript Events are a part of the Document Object Model (DOM) Level 3.
The below given are the HTML Event Handlers as per the HTML 4.01 specification.
<form> Level Events
The below given are the events which can be triggered on the Form Level
Form Attribute | Description |
---|---|
onchange | triggers when the element changes |
onsubmit | triggers when the form is submitted |
onreset | triggers when the form is reset |
onselect | triggers when the element is selected |
onblur | triggers when the element loses focus |
onfocus | triggers when the element gets focus |
<body> Level Events
The below given are the events which can be triggered on the Body or the Frameset Level
Body Attribute | Description |
---|---|
onload | triggers when a HTML document loads |
onunload | triggers when a HTML document unloads |
onresize | triggers when browser window is resized |
Keyboard Events
The below given are the events which can be triggered on the Keyboard Level
Keyboard Attribute | Description |
---|---|
onkeydown | triggers when key is pressed |
onkeypress | triggers when key is pressed and released |
onkeyup | triggers when key is released |
Mouse Events
Mouse Attribute | Description |
---|---|
onclick | triggers when a mouse click |
ondblclick | triggers when a mouse double-click |
onmousedown | triggers when mouse button is pressed |
onmousemove | triggers when mouse pointer moves |
onmouseout | triggers when mouse pointer moves out of an element |
onmouseover | triggers when mouse pointer moves over an element |
onmouseup | triggers when mouse button is released |
onClick Event Type Example in JavaScript
onClick Event Type is the most commonly used event in the UI application. Whenever the user click on the HTML elements the onClick event is triggered. The onClick event can be used to capture user input, perform form validations, displaying waning messages, etc.
<!DOCTYPE html> <html> <head> <script> function userMsg() { alert("The onClick Event Message is displayed when user clicks button "); } </script> </head> <body> <h3> JavaScript - onClick Event Type Example </h3> <button onclick = "userMsg()">Click me</button> </body> </html>
The below given is another onClick event in the paragraph element.
<HTML> <HEAD> </HEAD> <BODY> <h3> JavaScript - onClick Event Type Example </h3> <p id = "root">Click me</p> <script> document.getElementById("root").onclick = function() { userMsg() }; function userMsg() { document.getElementById("root").innerHTML = "The onClick Event Message is displayed when user clicks button "; document.getElementById("root").style.color = "Blue"; document.getElementById("root").style.backgroundColor = "Grey"; document.getElementById("root").style.fontSize = "15px"; document.getElementById("root").style.border = "4px solid Grey"; } </script> </BODY> </HTML>
onSubmit Event Type Example
When the Front End Application performs the “Submit” button, the onSubmit event type is triggered which could be used to validate the entered form details, submit request data as per the given action, displaying error and success messages, etc..
<html> <head> <script type = "text/javascript"> function validateFormDetails() { alert("Validate method executes with onSubmit Event Type "); } </script> </head> <body> <h3> JavaScript - onSubmit Event Type Example </h3> <form method = "POST" action = "" onsubmit = "return validateFormDetails()"> <input type = "submit" value = "Submit" /> </form> </body> </html>
HTML5 Standard Events
The below given is the complete list of events supported as part of HTML5 specification
Attribute | Description |
---|---|
Offline | Triggers when the document goes offline |
Onabort | Triggers on an abort event |
onafterprint | Triggers after the document is printed |
onbeforeonload | Triggers before the document loads |
onbeforeprint | Triggers before the document is printed |
onblur | Triggers when the window loses focus |
oncanplay | Triggers when media can start play, but might has to stop for buffering |
oncanplaythrough | Triggers when media can be played to the end, without stopping for buffering |
onchange | Triggers when an element changes |
onclick | Triggers on a mouse click |
oncontextmenu | Triggers when a context menu is triggered |
ondblclick | Triggers on a mouse double-click |
ondrag | Triggers when an element is dragged |
ondragend | Triggers at the end of a drag operation |
ondragenter | Triggers when an element has been dragged to a valid drop target |
ondragleave | Triggers when an element is being dragged over a valid drop target |
ondragover | Triggers at the start of a drag operation |
ondragstart | Triggers at the start of a drag operation |
ondrop | Triggers when dragged element is being dropped |
ondurationchange | Triggers when the length of the media is changed |
onemptied | Triggers when a media resource element suddenly becomes empty. |
onended | Triggers when media has reach the end |
onerror | Triggers when an error occur |
onfocus | Triggers when the window gets focus |
onformchange | Triggers when a form changes |
onforminput | Triggers when a form gets user input |
onhaschange | Triggers when the document has change |
oninput | Triggers when an element gets user input |
oninvalid | Triggers when an element is invalid |
onkeydown | Triggers when a key is pressed |
onkeypress | Triggers when a key is pressed and released |
onkeyup | Triggers when a key is released |
onload | Triggers when the document loads |
onloadeddata | Triggers when media data is loaded |
onloadedmetadata | Triggers when the duration and other media data of a media element is loaded |
onloadstart | Triggers when the browser starts to load the media data |
onmessage | Triggers when the message is triggered |
onmousedown | Triggers when a mouse button is pressed |
onmousemove | Triggers when the mouse pointer moves |
onmouseout | Triggers when the mouse pointer moves out of an element |
onmouseover | Triggers when the mouse pointer moves over an element |
onmouseup | Triggers when a mouse button is released |
onmousewheel | Triggers when the mouse wheel is being rotated |
onoffline | Triggers when the document goes offline |
onoine | Triggers when the document comes online |
ononline | Triggers when the document comes online |
onpagehide | Triggers when the window is hidden |
onpageshow | Triggers when the window becomes visible |
onpause | Triggers when media data is paused |
onplay | Triggers when media data is going to start playing |
onplaying | Triggers when media data has start playing |
onpopstate | Triggers when window history changes |
onprogress | Triggers when the browser fetches the media data |
onratechange | Triggers when the media data’s playing rate has changed |
onreadystatechange | Triggers when the ready-state changes |
onredo | Triggers when a redo operation is performed |
onresize | Triggers on the window resize |
onscroll | Triggers when an element’s scrollbar is being scrolled |
onseeked | Triggers when a media element’s seeking attribute is no longer true, and the seeking has ended |
onseeking | Triggers when a media element’s seeking attribute is true, and the seeking has begun |
onselect | Triggers when an element is selected |
onstalled | Triggers when there is an error in fetching media data |
onstorage | Triggers when a document loads |
onsubmit | Triggers when a form is submitted |
onsuspend | Triggers when the browser action is suspended |
ontimeupdate | Triggers when media changes its playing position |
onundo | Triggers when a undo operation is performed |
onunload | Triggers when the user leaves the document |
onvolumechange | Triggers when media changes the volume, also when volume is set to “mute” |
onwaiting | Triggers when media has stopped playing, but is expected to resume |