Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onmousedown — mouse-button press handler attribute

The onmousedown attribute assigns JavaScript code to an HTML element to handle the mousedown event. It responds to a mouse-button press rather than waiting for a completed click.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onmousedown="JavaScript statements"

onmousedown is a global event handler attribute available on HTML elements. Its value is a JavaScript function body run for the mousedown event. Do not confuse a mouse-button press with its release (mouseup) or with a click (click).

Inside the attribute code, event is the event being handled, and this refers to the element on which the handler is defined. In the example, this.textContent = event.type changes the button label to mousedown.

The HTML attribute contains code text; the corresponding JavaScript property element.onmousedown accepts a function or null. Content Security Policy can block code in event handler attributes. Do not insert untrusted data into such code.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onmousedown demo</title>
<button type="button" onmousedown="this.textContent = event.type">Press here</button>
</html>

Result

mousedown

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources