Execute Script on Mouse Button Press in HTML



Use the onmousedown attribute to in HTML to execute a script when a mouse button is pressed down on an element.

Example

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

<!DOCTYPE html>
<html>
   <body>
      <h3 id = "myid" onmousedown = "mouseDown()" onmouseup = "mouseUp()">
         This is demo heading.
      </h3>
      <p>Click above and then release.</p>
      <script>
         function mouseDown() {
            document.getElementById("myid").style.color = "yellow";
         }
         function mouseUp() {
            document.getElementById("myid").style.color = "blue";
         }
      </script>
   </body>
</html>
Updated on: 2020-03-03T12:37:45+05:30

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements