1

Is it possible that I can assign a callback function for a right click while I disable the context menu?

FIDDLE

$('div').on('contextmenu', function (e) {
    e.preventDefault();
    return false;
});

$('label').on('click', function (e) {
    var btn = e.button;
    if (btn == 2) {
        alert(1);
    } else {
        return false;
    }
});

2 Answers 2

2

this would be a much more convenient way to do it: DEMO

$('div').on('contextmenu', function (e) {
    if(!$(this).children('label').is(e.target)){
        e.preventDefault();
        return false;
    }
});

$('label').on('contextmenu', function (e) {
    alert(1);
    e.preventDefault();
    return false;
});
Sign up to request clarification or add additional context in comments.

Comments

1

You're almost there. The contextmenu event fires when you right click. So in that handler, you should do what you want and then e.preventDefault().

$('div').on('contextmenu', function (e) {
    var btn = e.button;
    if (btn == 2) {
        alert(1);
    } else {
        return false;
    }

    e.preventDefault();
    return false;
});

Comments

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.