How to simulate right click in javascript

Ok, so I know I can simulate a click by running this code

document.getElementById('recover').click(); 

the closest i could find was cntextmenu so i tried

 document.getElementById('recover').contextmenu(); 

however it does nothing

Can I right-click an element to bring up a context menu so that I can click an element in this list? and if so, could someone point me in the right direction to achieve such a goal?

I did a few searches, but the only thing I found was jquery javascript, capturing an event that doesn't actually fire the event

+4
source share
2 answers

with jQuery

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

otherwise

 var element = document.getElementById('recover'); var e = element.ownerDocument.createEvent('MouseEvents'); e.initMouseEvent('contextmenu', true, true, element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false,2, null); return !element.dispatchEvent(e); 
+6
source

Of course you can use the jQuery trigger () function.

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

Depending on what you are doing, you can program a mouse, and then a mouse, which may look like this:

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

I am not a big fan of such long teams, but everything that is most readable for your application is wonderful.

+3
source

Source: https://habr.com/ru/post/1486058/


All Articles