SwiftMango
SwiftMango

Reputation: 15284

How can I disable an element's context menu without disable the contextMenu for the entire document?

I have this code to disable context menu on a div

    disableContextMenu = function (e){ 
        if(e.stopPropagation)
            e.stopPropagation();
        if(e.preventDefault)
            e.preventDefault();
        e.cancelBubble = true;
        return false;
    };
    clickedElement.oncontextmenu = disableContextMenu;
    if(clickedElement.addEventListener){
        clickedElement.addEventListener('contextmenu', disableContextMenu, false);
    }else if(clickedElement.attachEvent){
        clickedElement.attachEvent('oncontextmenu', disableContextMenu);
    }

This works as expect. But when I add a mouseup handler, the context menu comes up again.

        clickedElement.onmousedown = function (e){
        if(e.which == 3){
            if(e.stopPropagation)
                e.stopPropagation();
            if(e.preventDefault)
                e.preventDefault();
            elementToShow.style.position = 'absolute';
            elementToShow.style.left = e.pageX + 'px';
            elementToShow.style.top = e.pageY + 'px';
            elementToShow.style.display = 'block';
            return false;
        }
    }

This does not work in my IE9. I used document instead and it works, but I don't want to disable the context menus for the textboxes and other elements. How can I do that?

Do not suggest me not to use this. It is a project for client and this is a requirement.

Upvotes: 0

Views: 262

Answers (1)

helly0d
helly0d

Reputation: 818

You could check also for the target of the event and disable context menu only if that target is the object on which you do not want it.

Upvotes: 1

Related Questions