0

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/

3 Answers 3

1

Remove the code to handle right clicks from within event listener for the Toggle Right Click button.

const $document = $(document);

$document.ready(() => {
  let toggleValue;

  $('.btn-toggle').on('click', () => {
    toggleValue = !toggleValue;
  });

  $document.on('contextmenu', (e) => {
    if (toggleValue) {
      e.preventDefault();
    }
  });
});

Also, you can change let $document = $(document); to const $document = $(document); as you won't be assigning a different value to this variable (at least you shouldn't be).

Edit: updated in response to comment.

Sign up to request clarification or add additional context in comments.

2 Comments

This will make the right click disabled by default. I want it to be disable/enable by button click
Just change the let toggleValue = true; to let toggleValue;. I'll update my answer.
1

You reassign click handlers over and over - try assign the click handler just once.

let $document = $(document);        
    $document.ready(() => {
        let toggleValue = true;
        $document.on('contextmenu', (e) => {
                if(toggleValue) {
                    e.preventDefault();
                }
            });

        $('.btn-toggle').on('click', () => {            
            toggleValue = !toggleValue;
        })
    })

2 Comments

This will make the right click disabled by default. I want it to be disable/enable by button click
Start with let toggleValue = false then.
0

simply use if(toggleValue === false) instead of if(toggleValue)

let $document = $(document);		
$document.ready(() => {
  let toggleValue = true;
  $('.btn-toggle').on('click', () => {
    $document.on('contextmenu', (e) => {
      if(toggleValue === false) {
        e.preventDefault();
      }
    });
    toggleValue = !toggleValue;
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Toggle right click menu on button click</h3>

<button class="btn-toggle">Toggle Right Click</button>

1 Comment

As Evan points out, with this solution, you are assigning click handlers over and over...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.