Computer >> Computer tutorials >  >> Programming >> Javascript

What are events in JavaScript?


JavaScript's interaction with HTML is handled through events that occur when 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 any key, closing a window, resizing a window, etc. Events are a part of the Document Object Model (DOM) Level 3 and every HTML element contains a set of events which can trigger JavaScript Code.

Let’s see an example of onclick event in JavaScript, which is the most frequently used event type which occurs when a user clicks the left button of his mouse.

Example

You can put your validation, warning etc., against this event type.

<html>
   <head>
      <script>
         <!--
            function sayHello() {
               alert("Hello World")
            }
         //-->
      </script>
   </head>
   <body>
      <p>Click the following button and see result</p>

      <form>
         <input type = "button" onclick = "sayHello()" value = "Say Hello" />
      </form>
   </body>
</html>