onmousedown Event

Example

Call a function when pressing a mouse button over a paragraph:

<p onmousedown="myFunction()">Click the text!</p>
Try it Yourself »

More examples below.


Description

The onmousedown event occurs when a user presses a mouse button over an HTML element.

Events order for the left and middle mouse button:

  1. onmousedown
  2. onmouseup
  3. onclick

Events order for the right mouse button:

  1. onmousedown
  2. onmouseup
  3. oncontextmenu

Mouse Events

EventOccurs When
onclick The user clicks on an element
oncontextmenu The user right-clicks on an element
ondblclick The user double-clicks on an element
onmousedown A mouse button is pressed over an element
onmouseenter The pointer is moved onto an element
onmouseleave The pointer is moved out of an element
onmousemove The pointer is moving over an element
onmouseout The mouse pointer moves out of an element
onmouseover The mouse pointer is moved over an element
onmouseup The mouse button is released over an element

See Also:

The Mouse Event Object

Tutorial:

JavaScript Events



Syntax

In HTML:

<element onmousedown="myScript">
Try it Yourself »

In JavaScript:

object.onmousedown = function(){myScript};
Try it Yourself »

In JavaScript, using the addEventListener() method:

object.addEventListener("mousedown", myScript);
Try it Yourself »

Technical Details

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

More Examples

Trigger a function with parameters when the button is pressed down
When the mouse button is pressed down over a <p> element, change its color to red.

Alert which mouse button that was pressed
Alert which mouse button the user pressed.

Alert the element the user clicked on
Alert the name of the element the user clicked on.


Browser Support

onmousedown is a DOM Level 2 (2001) feature.

It is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.