JavaScript Events.

JavaScript Events.

ยท

3 min read

JavaScript is meant to add interactivity to your pages. javaScript does this using a mechanism using events. Events are a part of the Document Object Model (DOM) and every HTML element contains a set of events that can trigger JavaScript code.

An event is an action or occurrence recognized by the software. It can be triggered by a user or the system. Some common examples of events include a user clicking on a button, loading the web page, clicking on a hyperlink and so on.

Following are some of the common HTML events.

Event Handlers

On the occurrence of an event, the application executes a set of relatd tasks. The block of code that achieves this purpose is called the event handler. Every HTML elements has a set of events associated with it. We can define how the events will be processed in JavaScript by using event handlers.

onclick Event Types

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

Example:

<html>
   <head>
        <script type = "text/javascript">
             function sayHello() {
                      document.write ("hello world")
                               }
</script>
</head>

<body>
<p> click the following button and see result </p>
   <input type = "button" onclick = "sayhello()" value = "say hello" />
</body>
</html>

onsubmitEvent Type

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

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

Example:

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

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

onmouseover and onmouseout

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

Example:

<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 the result: </p>
<div onmouseover = "over()" onmouseout = "out()">
<h2> This is inside the division </h2>
</div>
</body>
</html>

HTML5 Standard Events

The standard HTML 5 events are listed in the following table for your reference. The script indicates a JavaScript Function to be executed against that event.

events.JPG

Did you find this article valuable?

Support Olabode Olusegun by becoming a sponsor. Any amount is appreciated!