Execute a script when the element is being clicked in HTML?

HTMLWeb DevelopmentFront End Technology

Use the onclick attribute to execute a script when the element is clicked in HTML.

Example

You can try to run the following code to implement onclick attribute −

<!DOCTYPE html>
<html>
   <body>
      <button onclick = "display()">Click</button>

      <p id = "test"></p>

      <script>
         function display() {
            document.getElementById("test").innerHTML = "You clicked the button!";
         }
      </script>
   </body>
</html>
raja
Published on 14-Mar-2018 16:10:37
Advertisements