This project is archived and is in readonly mode.

#158 ✓wontfix

Add a string 'button' property to Event (patch)

Reported by adammck | June 23rd, 2008 @ 10:30 PM | in 2.0 (closed)

When a mouse(down|up) event is fired, there isn't any easy way to find out which mouse button was pressed. There is already an undocumented "rightClick" property, but this doesn't support the middle button, and is kind of ugly, IMHO. It's also possible to inspect the original event object, but this is fraught with cross-browser quirks.

This patch adds the mouse button as a string (left, right, or middle), and updates the docs. Tested in IE6+7, Firefox 2+3, Safari 3.1, and Opera 9.5.

Example usage:

$(document).addEvent ("mousedown", function (evt) {
    alert ("You pressed the " + evt.button + " mouse button!");

Comments and changes to this ticket

  • Jan Kassens

    Jan Kassens June 23rd, 2008 @ 10:47 PM

    • State changed from “new” to “hold”

    I'm pretty sure middle button detection isn't cross-browser.

    Setting this ticket to hold, so this can be checked.

  • adammck

    adammck June 23rd, 2008 @ 10:58 PM

    All four major engines support it, so I'd say it's pretty cross-browser. Also, I quote from The Powers that Be

    button of type unsigned short, readonly

    During mouse events caused by the depression or release of a mouse button, button is used to indicate which mouse button changed state. The values for button range from zero to indicate the left button of the mouse, one to indicate the middle button if present, and two to indicate the right button. For mice configured for left handed use in which the button actions are reversed the values are instead read from right to left.

  • adammck

    adammck June 23rd, 2008 @ 11:19 PM

    Better patch attached. Tested in the same browsers.

  • Christoph Pojer

    Christoph Pojer November 9th, 2010 @ 07:25 PM

    • Tag set to enhancement, events, patch
    • State changed from “hold” to “wontfix”
    • Milestone order changed from “0” to “0”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket