Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger right-click

I am trying to late-bind context menus to elements, using the ContextMenu plugin. So on the first right-click on those elements, I would like to :

  1. intercept the right-click through a live event on a certain "uncontextmenued" class,
  2. determine if the data('events').contextmenu exists,
  3. if not, attach the context-menu (and change the class to avoid re-throwing this live process),
  4. re-throw the right-click event to show the right-click.

I'm having trouble with the last item. jQuery allows to .click() or to .trigger('click'), which simulate a left-click, but there seems not to be a way to fire a right-click event through trigger.

Or is there?

like image 512
glmxndr Avatar asked Jun 06 '11 10:06

glmxndr


People also ask

How do I add a right click event?

We will use the oncontextmenu property to listen to the right-click events. We will create a rightClick() function. In this function, we will call the preventDefault() method of the mouse click event which will cancel the default behavior of the right-click event. We can also return “false” to cancel the event.

How do you trigger a context menu?

The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.

How do you handle right click?

Set the window.contextmenu property to an event handler function to listen for right clicks on the page. We have the e parameter that has the right-click event object. We call e. preventDefault to stop the default behavior for right clicks, which is to show the context menu on the page.

How do I choose the right mouse click?

You can detect which button has been clicked by checking the MouseEvent. button property. This property will be available on mouse clicking events like: mousedown , mouseup , click , dblclick , contextmenu . The MouseEvent.


1 Answers

You can trigger it by

$('#element').trigger({
    type: 'mousedown',
    which: 3
});

http://api.jquery.com/trigger/#example-5

like image 169
Niclas Sahlin Avatar answered Oct 24 '22 03:10

Niclas Sahlin