Event Handling in JavaScript

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 AttributeDescription
onchangetriggers when the element changes
onsubmittriggers when the form is submitted
onresettriggers when the form is reset
onselecttriggers when the element is selected
onblurtriggers when the element loses focus
onfocustriggers 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 AttributeDescription
onloadtriggers when a HTML document loads
onunloadtriggers when a HTML document unloads
onresizetriggers when browser window is resized
Keyboard Events

The below given are the events which can be triggered on the Keyboard Level

Keyboard AttributeDescription
onkeydowntriggers when key is pressed
onkeypresstriggers when key is pressed and released
onkeyuptriggers when key is released
Mouse Events
Mouse AttributeDescription
onclicktriggers when a mouse click
ondblclicktriggers when a mouse double-click
onmousedowntriggers when mouse button is pressed
onmousemovetriggers when mouse pointer moves
onmouseouttriggers when mouse pointer moves out of an element
onmouseovertriggers when mouse pointer moves over an element
onmouseuptriggers 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

AttributeDescription
OfflineTriggers when the document goes offline
OnabortTriggers on an abort event
onafterprintTriggers after the document is printed
onbeforeonloadTriggers before the document loads
onbeforeprintTriggers before the document is printed
onblurTriggers when the window loses focus
oncanplayTriggers when media can start play, but might has to stop for buffering
oncanplaythroughTriggers when media can be played to the end, without stopping for buffering
onchangeTriggers when an element changes
onclickTriggers on a mouse click
oncontextmenuTriggers when a context menu is triggered
ondblclickTriggers on a mouse double-click
ondragTriggers when an element is dragged
ondragendTriggers at the end of a drag operation
ondragenterTriggers when an element has been dragged to a valid drop target
ondragleaveTriggers when an element is being dragged over a valid drop target
ondragoverTriggers at the start of a drag operation
ondragstartTriggers at the start of a drag operation
ondropTriggers when dragged element is being dropped
ondurationchangeTriggers when the length of the media is changed
onemptiedTriggers when a media resource element suddenly becomes empty.
onendedTriggers when media has reach the end
onerrorTriggers when an error occur
onfocusTriggers when the window gets focus
onformchangeTriggers when a form changes
onforminputTriggers when a form gets user input
onhaschangeTriggers when the document has change
oninputTriggers when an element gets user input
oninvalidTriggers when an element is invalid
onkeydownTriggers when a key is pressed
onkeypressTriggers when a key is pressed and released
onkeyupTriggers when a key is released
onloadTriggers when the document loads
onloadeddataTriggers when media data is loaded
onloadedmetadataTriggers when the duration and other media data of a media element is loaded
onloadstartTriggers when the browser starts to load the media data
onmessageTriggers when the message is triggered
onmousedownTriggers when a mouse button is pressed
onmousemoveTriggers when the mouse pointer moves
onmouseoutTriggers when the mouse pointer moves out of an element
onmouseoverTriggers when the mouse pointer moves over an element
onmouseupTriggers when a mouse button is released
onmousewheelTriggers when the mouse wheel is being rotated
onofflineTriggers when the document goes offline
onoineTriggers when the document comes online
ononlineTriggers when the document comes online
onpagehideTriggers when the window is hidden
onpageshowTriggers when the window becomes visible
onpauseTriggers when media data is paused
onplayTriggers when media data is going to start playing
onplayingTriggers when media data has start playing
onpopstateTriggers when window history changes
onprogressTriggers when the browser fetches the media data
onratechangeTriggers when the media data’s playing rate has changed
onreadystatechangeTriggers when the ready-state changes
onredoTriggers when a redo operation is performed
onresizeTriggers on the window resize
onscrollTriggers when an element’s scrollbar is being scrolled
onseekedTriggers when a media element’s seeking attribute is no longer true, and the seeking has ended
onseekingTriggers when a media element’s seeking attribute is true, and the seeking has begun
onselectTriggers when an element is selected
onstalledTriggers when there is an error in fetching media data
onstorageTriggers when a document loads
onsubmitTriggers when a form is submitted
onsuspendTriggers when the browser action is suspended
ontimeupdateTriggers when media changes its playing position
onundoTriggers when a undo operation is performed
onunloadTriggers when the user leaves the document
onvolumechangeTriggers when media changes the volume, also when volume is set to “mute”
onwaitingTriggers when media has stopped playing, but is expected to resume