JSFiddle - React, Tailwind, and code Playground
by Federico Ghedina
HTML
<body>
<div id="trg">hello do not click me right</div>
<div>hello click me right</div>
</body>
CSS
div{
border:1px solid red;
padding:10px;
}
JavaScript
function onright(el, cb) {
//disable right click
document.body.oncontextmenu = 'return false';
el.addEventListener('contextmenu', function (e) {e.preventDreturn false});
el.addEventListener('mousedown', function (e) {
e = e || window.event;
if (~~(e.button) === 2) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
return false;
}
});
// then bind Your cb
el.addEventListener('mousedown', function (e) {
e = e || window.event;
~~(e.button) === 2 && cb.call(el, e);
});
}
onright(document.getElementById('trg'), function (e) {
console.log(e);
})