How to call a JavaScript function on a click event?

JavascriptWeb DevelopmentFront End Scripts

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 clicks to is an event. Other examples include events like pressing any key, closing a window, resizing a window, etc.

The onclick() event occurs when a user clicks the left button of his mouse. Place your validation, warning etc., against this event type.

Example

You can try to run the following code to call a JavaScript function on a click event −

Live Demo

<html>
   <head>
      <script>
         <!--
            function sayHello() {
               alert("Demo Alert")
            }
         //-->
      </script>
   </head>
   <body>
      <p>Click the following button and see result</p>
      <form>
         <input type="button" onclick="sayHello()" value="Click" />
      </form>
   </body>
</html
raja
Published on 07-Feb-2018 12:59:31
Advertisements