I would like to know how I could make a left-click behave as a right-click in javascript. I would like that, by doing a left click, the context menu appears.
Regards.
I would like to know how I could make a left-click behave as a right-click in javascript. I would like that, by doing a left click, the context menu appears.
Regards.
The event listener:
document.addEventListener('click', function(e) {
console.log(e);
});
This gives you already a lot of information. You'll get the x/y coords of your mouseclick and more important, you get the element that received the click: e.originalTarget (note that events differ from browser to browser).
save the element:
document.addEventListener('click', function(e) {
var originalElement = e.srcElement || e.originalTarget;
});
and fire a right-click on that element:
document.addEventListener('click', function(e) {
var originalElement = e.srcElement || e.originalTarget;
if (document.createEvent) {
var ev = document.createEvent('HTMLEvents');
ev.initEvent('contextmenu', true, false);
originalElement .dispatchEvent(ev);
} else { // Internet Explorer
originalElement .fireEvent('oncontextmenu');
}
});
it does not seem to work yet, so consider using this method for firing the right-click in pure js: How to generate a right-click event in all browsers
try this
$(document).contextmenu(function (event) {
$(event.target).click();
event.preventDefault();
})