Following is my jQuery Code which is working fine except for the first click. I am expecting the right click to be disabled on the first click, but somehow first click is not disabling the right click but after that toggle works. Let me know what I am doing wrong here.
jQuery Code -
let $document = $(document);
$document.ready(() => {
let toggleValue = true;
$('.btn-toggle').on('click', () => {
$document.on('contextmenu', (e) => {
if(toggleValue) {
e.preventDefault();
}
});
toggleValue = !toggleValue;
})
})
HTML Code -
<h3>Toggle right click menu on button click</h3>
<button class="btn-toggle">Toggle Right Click</button>
JSFIDDLE - https://jsfiddle.net/mLm096o4/