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.


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

Live Demo

            function sayHello() {
               alert("Demo Alert")
      <p>Click the following button and see result</p>
         <input type="button" onclick="sayHello()" value="Click" />
Updated on 17-Jun-2020 06:25:47