June 4, 2023

JavaScript – Events

JavaScript could also be called event-driven. Hence it can respond to events. However, let’s first understand what events are.

  1. What are Events
  2. The onclick() Event
  3. The onsubmit() Event
  4. The mouseover() and mouseout() event
  5. List of HTML5 Events

 

1. What are Events

JavaScript code interacts with the HTML via events that are generated when a user or the browser manipulates the page.

For example, when the browser loads a page, this is an event. When a user clicks a button in a html page, this is also an event. Other events includes: pressing a key, resizing a window, closing a window etc.

Programmers can use these events as a trigger to execute a JavaScript code as a response. So when a user clicks on a button, the programmer specifies the codes to run. Generally this is a function as we learned in Tutorial 10.

Events in JavaScript are part of  the Document Object Model (DOM) Level 3. Therefore, every HTML element contains a collection of events which can be used to trigger a JavaScript code.

Let’s start with an onclick() event

 

2. onclick() Event

The onclick() event happens when the mouse is clicked. This is the most frequently used event. You can actually put some validation code to run before the actual action is performed.

The example below demonstrates the onclick event

<html>
   <head>  
   <title>Events Demo</title>   
      <script type = "text/javascript">
         <!--
            function greetings() {
               alert("Good Morning")
            }
         //-->
      </script>      
   </head>
   
   <body>
      <p>Click the following button and see result</p>      
      <form>
         <input type = "button" onclick = "greetings()" value = "Good Morning" />
      </form>      
   </body>
</html>

 

3. onsubmit() Event

This is is an event that occurs when you try to submit a form. Therefore, you can put some validation codes before the actual actions is performed.

For example, the code in the example below show the validation of a form when a submit button is clicked. Remember that if you click the submit button in a form, the data entered in the form is submitted to the server. Won’t it be great to validate the content of the form before you submit.

The example below illustrates this.

<html>
   <head>  
   <title>Events Demo</title>   
      <script type = "text/javascript">
         <!--
            function validation() {
               all validation goes here
               .........
               return either true or false
            }
         //-->
      </script>     
   </head>
   
   <body>
      <p>Click the following button and see result</p>      
      <form method = "POST" action = "#" onsubmit = "return validate()">
         .......
         <input type = "submit" value = "Submit" />
      </form>        
   </body>
</html>

 

4. onmouseover() and onmouseout() events

These are events used to create a hover effect when the mouse moves over a control on a html page. For example, you may want to brighten an images color when the mouse moves over it.

The onmouseover() event triggers when the moves over an element while onmouseout() triggers when the mouse moves out of the that element.

The example below illustrates this

<html>
   <head>  
   <title>moouseover and mouseout Demo</title>   
      <script type = "text/javascript">
         <!--
            function over() {
               document.write ("Mouse Over");
            }            
            function out() {
               document.write ("Mouse Out");
            }            
         //-->
      </script>     
   </head>
   
   <body>
      <p>Take your mouse inside this section to see the outcome:</p>      
      <div onmouseover = "over()" onmouseout = "out()">
         <h2> This is inside the section </h2>
      </div>       
   </body>
</html>

 

5. List of HTML 5 Events

The table below provides a list of standard events in HTML5

Event/AttributeValueBrief Description
OfflinescriptTriggers when the document goes offline.
OnabortscriptTriggers on an abort event.
onafterprintscriptTriggers after the document is printed
onbeforeonloadscriptTriggers before the document loads
onbeforeprintscriptTriggers before the document is printed
onblurscriptTriggers when the window loses focus
oncanplayscriptTriggers when media can start play, but might has to stop for buffering
oncanplaythroughscriptTriggers when media can be played to the end, without stopping for buffering
onchangescriptTriggers when an element changes
onclickscriptTriggers on a mouse click
oncontextmenuscriptTriggers when a context menu is triggered
ondblclickscriptTriggers on a mouse double-click
ondragscriptTriggers when an element is dragged
ondragendscriptTriggers at the end of a drag operation
ondragenterscriptTriggers when an element has been dragged to a valid drop target
ondragleavescriptTriggers when an element is being dragged over a valid drop target
ondragoverscriptTriggers at the start of a drag operation
ondragstartscriptTriggers at the start of a drag operation
ondropscriptTriggers when dragged element is being dropped
ondurationchangescriptTriggers when the length of the media is changed
onemptiedscriptTriggers when a media resource element suddenly becomes empty.
onendedscriptTriggers when media has reach the end
onerrorscriptTriggers when an error occur
onfocusscriptTriggers when the window gets focus
onformchangescriptTriggers when a form changes
onforminputscriptTriggers when a form gets user input
onhaschangescriptTriggers when the document has change
oninputscriptTriggers when an element gets user input
oninvalidscriptTriggers when an element is invalid
onkeydownscriptTriggers when a key is pressed
onkeypressscriptTriggers when a key is pressed and released
onkeyupscriptTriggers when a key is released
onloadscriptTriggers when the document loads
onloadeddatascriptTriggers when media data is loaded
onloadedmetadatascriptTriggers when the duration and other media data of a media element is loaded
onloadstartscriptTriggers when the browser starts to load the media data
onmessagescriptTriggers when the message is triggered
onmousedownscriptTriggers when a mouse button is pressed
onmousemovescriptTriggers when the mouse pointer moves
onmouseoutscriptTriggers when the mouse pointer moves out of an element
onmouseoverscriptTriggers when the mouse pointer moves over an element
onmouseupscriptTriggers when a mouse button is released
onmousewheelscriptTriggers when the mouse wheel is being rotated
onofflinescriptTriggers when the document goes offline
onoinescriptTriggers when the document comes online
ononlinescriptTriggers when the document comes online
onpagehidescriptTriggers when the window is hidden
onpageshowscriptTriggers when the window becomes visible
onpausescriptTriggers when media data is paused
onplayscriptTriggers when media data is going to start playing
onplayingscriptTriggers when media data has start playing
onpopstatescriptTriggers when the window’s history changes
onprogressscriptTriggers when the browser is fetching the media data
onratechangescriptTriggers when the media data’s playing rate has changed
onreadystatechangescriptTriggers when the ready-state changes
onredoscriptTriggers when the document performs a redo
onresizescriptTriggers when the window is resized
onscrollscriptTriggers when an element’s scrollbar is being scrolled
onseekedscriptTriggers when a media element’s seeking attribute is no longer true, and the seeking has ended
onseekingscriptTriggers when a media element’s seeking attribute is true, and the seeking has begun
onselectscriptTriggers when an element is selected
onstalledscriptTriggers when there is an error in fetching media data
onstoragescriptTriggers when a document loads
onsubmitscriptTriggers when a form is submitted
onsuspendscriptTriggers when the browser has been fetching media data, but stopped before the entire media file was fetched
ontimeupdatescriptTriggers when media changes its playing position
onundoscriptTriggers when a document performs an undo
onunloadscriptTriggers when the user leaves the document
onvolumechangescriptTriggers when media changes the volume, also when volume is set to “mute”
onwaitingscriptTriggers when media has stopped playing, but is expected to resume

We recommend you try out some of these events just to see how they work!

1 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x