How to call a JavaScript function from an onClick event?


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

Example

You can try to run the following code to call a JavaScript function from an onClick event

Live Demo

<html>
   <head>
      <script>
         <!--
            function display() {
               alert("Hello World")
            }
         //-->
      </script>
   </head>
   <body>
      <p>Click the following button and see result</p>
      <form>
         <input type="button" onclick="display()" value="Click me" />
      </form>
   </body>
</html>

Updated on: 08-Jan-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements