Search code examples
javascriptright-clickonmousedownselectonemenumousedown

How can I make a right-click behave as a left-click for the purpose of selecting or focusing an object


Event though a right-click (as far as I know) fires a mousedown event, that mousedown seems to be ignored in most cases. I'm currently working on displaying a custom context menu via a right-click, but I'd also like to be able to select an option from a list while I'm right-clicking. As of right now my recognizes the click from both buttons enough to run some javascript tied to the onmousedown attribute but not enough to select the option the mouse is over when the mousedown comes from the right button.

Is there a way to bypass a browser's default behavior of ignoring the mousedown event of a right-click or fool it into thinking the mousedown was generated by the left button instead?

Thanks in advance.


Solution

  • You can use the oncontextmenu event.

    Edit: To simulate the default click behavior on an <option> during a right mouse click, put this code in your event handler when handling right click:

    clickedOption.parentNode.selectedIndex = clickedOption.index;