Sunday, December 8, 2019

JavaScript Events tutorial with examples


Hello morning to you all, welcome to my new tutorial on JavaScript. In my last tutorial post we rounded up all about JavaScript function.

In this tutorial we are going to be discussing Events in JavaScript. JavaScript's interaction with HTML is handled through events that do occur whenever the user or the browser manipulates a page

When the page loads, it is called an event. When the user clicks a button, that click too is an event. Other examples include events like pressing a key, closing a window, resizing a window, etc.

Developers can make use of these events to execute the JavaScript coded responses, which in return cause buttons to close windows, messages to be displayed to the users, information to be validated, and virtually any other type of responses imaginable.

Events are a part of the Document Object Model (DOM) Level 3 and every Html element contains a set of events which can be used to trigger Javascript code. Let's take a look at few examples below in order to learn and understand the relationship between the Event and JavaScript.

You can also read our tutorial post on: Learn how to implement JavaScript Literals with examples


onclick Event Type

This is the most often used event type which occurs when the user clicks on the left button of his/her mouse. You can put your warning, validation, etc., against this event type.

Example

Try the following example below for better understanding.

<html>
     <head>
          <script  type = "text/javascript">
               <!--
                    function  sayHello()  {
                         alert("Hello World");
                    }
               //-->
          </script>
     </head>

     <body>
          <p>Click the following button to see result</p>
          <form>
               <input  type = "button"  onclick = "sayHello()"  value = "Click" />    
          </form>
     </body>
</html>

Output

Below is the output of the above example.

Click the following button to see result 

  



Onsubmit Event Type

Onsubmit is an event that occurs when you try to submit a form. You can put in your form validation against this event type.

Example

The following example shows how to use onsubmit. Here we are calling a validate() function before submitting a form data to the web server. If validate() method returns true, the form will be submitted, otherwise it will not submit the data.

Try the following example below for better understanding.

<html>
     <head>
          <script  type = "text/javascript">
               <!--
                    function  validation()  {
                         all validation goes here
                         ..........................................
                         returns either true or false 
                    }
               //-->
          </script>
     </head>

     <body>
          <form  method = "POST"  action = "t.cgi"  onsubmit = "return validate()">      
               ...........................................
               <input  type = "submit"  value = "Submit" />
          </form>
     </body>
</html> 

You can also read our tutorial post on: JavaScript - while Loop


onmouseover and onmouseout
These two event types is going to help you create nice effects with images or even with text as well. The onmouseover event triggers when you bring your mouse over any element and the onmouseout event triggers when you move your mouse out from that element.

Try the following example below for better understanding.

<html>
     <head>
          <script  type = "text/javascript">
               <!--
                    function  over()  {
                         document.write("Mouse Over");
                    }
                    function  out()  {
                         document.write("Mouse Out");
                    }
               //-->
          </script>
     </head>

     <body>
          <p>Bring your mouse inside the division to see result:</p>    
          <div  onmouseover = "over()"  onmouseout = "out()">
               <h1>This is inside the division</h1>
          </div>
     </body>
</html>

Output
Below is the output of the above example

Bring your mouse inside the division to see result 

This is inside the division 


HTML 5 Standard Events
The HTML 5 standard events are listed below for your reference. Here script indicates a JavaScript function to be executed against that event.

AttributeValueDescription
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
Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be studying about Javascript and cookies.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.