0

I want to add custom context menu with jQuery for the whole body of the page, except the textfields. How can I do that? I have tried that code:

$('body:not(input)').bind('contextmenu', function(){
    /*code*/
});
4

3 Answers 3

2

Check the srcElement before plugin executions. If it's not an input element, do trigger the contextmenu plugin:

$(document).on("contextmenu", function(e) {
    if (!$(e.srcElement).is(":input")) { // if it's not an input element...
        $(this).triggerTheContextMenuPlugin();
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

e.target and e.srcElement return the same value.
1

Use an event listener on the document and check if it was initiated by an input element.

$(document).on("contextmenu", function (e) {
    if (e.target.tagName.toUpperCase() === "INPUT") {
        console.log("context menu triggered");
    }
});

Demo here

Comments

0

Inspired by Salman's solution.

You can stop the event propagation in all input elements, with the e.stopPropagation() function. In doing so, you keep the default behavior of the inputs elements:

$(function() {
    $(document).on("contextmenu", function(e) {
        alert("Context menu triggered, preventing default");
        e.preventDefault();
    });
    $("input").on("contextmenu", function(e) {
        e.stopPropagation();
    });
});

JSFiddle Demo

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.