My jQuery code is mostly working fine, but I'm having an issue with the first click not disabling the right-click as expected. After the initial click, everything works as intended. Can someone help me figure out what's going wrong?
Here's the jQuery Code:
let $document = $(document);
$document.ready(() => {
let toggleValue = true;
$('.btn-toggle').on('click', () => {
$document.on('contextmenu', (e) => {
if(toggleValue) {
e.preventDefault();
}
});
toggleValue = !toggleValue;
})
})
And here's the corresponding HTML code:
<h3>Toggle right-click menu on button click</h3>
<button class="btn-toggle">Toggle Right Click</button>
You can check out the JSFIDDLE for reference: https://jsfiddle.net/mLm096o4/