onmousedown Event

Definition and usage

An onmousedown event occurs when the user presses the mouse button on an element.

Tip:Event sequence related to onmousedown event (for left mouse button/middle mouse button):

  1. onmousedown
  2. onmouseup
  3. onclick

Event sequence related to onmousedown event (for right mouse button):

  1. onmousedown
  2. onmouseup
  3. oncontextmenu

Example

Execute JavaScript when the mouse button is pressed on the paragraph:

<p onmousedown="myFunction()">Click the text!</p>

Try it yourself

More TIY examples are at the bottom of the page.

Syntax

In HTML:

<element onmousedown="myScript">

Try it yourself

In JavaScript:

object.onmousedown = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("mousedown", myScript);

Try it yourself

Note:Internet Explorer 8 and earlier versions do not support addEventListener() method.

Technical details

Bubble: Supported
Cancelable: Supported
Event type: MouseEvent
Supported HTML tags: All HTML elements, except: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> and <title>
DOM Version: Level 2 Events

Browser support

Event Chrome IE Firefox Safari Opera
onmousedown Supported Supported Supported Supported Supported

More examples

Triggers a function with parameters when the button is pressed
When the mouse button is pressed on the <p> element, change its color to red.
Remind which mouse button was pressed.
Remind the user which mouse button was pressed.
Prompt the element clicked.
Remind the user of the name of the element clicked.