How to call a JavaScript function on click?

JavascriptWeb DevelopmentFront End Technology

To call a function on click of a button in JavaScript, use onclick. Try to run the following code to call a function onclick in JavaScript −

Example

<html>
   <head>
      <script>
         function sayHello() {
            document.write ("Hello there!");
         }
      </script>
   </head>
   <body>
      <p>Click the following button to call the function</p>

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

      <p>Use different text in write method and then try...</p>
   </body>
</html>
raja
Published on 16-Feb-2018 15:40:58
Advertisements