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
Updated on 03-Mar-2020 12:32:25

Advertisements