onmousedown

The onmousedown event is triggered when a mouse button is pressed down on an element.

onmousedown event

The onmousedown event attribute activates a <script> when a mouse button is pressed on an element. It's often used to manage interactions within user interface elements.

Syntax

In HTML

index.html
<element onmousedown="myScript">

In JS

script.js
object.onmousedown = function(){myScript};

In JavaScript, the addEventListener() function is used.

script.js
object.addEventListener("mousedown", myScript);

Example

::event-onmousedown ::
index.html
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onmousedown Event</h2>
<p>Clock the text below!</p>
<p id="myP" onmousedown="mouseDown()" onmouseup="mouseUp()">
The mouseDown() function sets the color of this text to red.
The mouseUp() function sets the color of this text to blue.
</p>

<script>
function mouseDown() {
  document.getElementById("myP").style.color = "red";
}

function mouseUp() {
  document.getElementById("myP").style.color = "blue";
}
</script>

</body>
</html>

Values

Conclusion

The onmousedown event is triggered when a mouse button is pressed on an element. It's commonly used for handling interactions in UI components. This event allows you to execute scripts in response to user actions.