Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable contextmenu but NOT in textboxes

I want to disable the contextmenu on my page, because I have an own. But in the textboxes it should be possible to use the system contextmenu.

I'm doing it with Javascript like that:

document.oncontextmenu = function (e) {
    return false;
};

I thought about something like:

document.oncontextmenu = function (e) {
    if (e.taget.nodeName != "text") {
    return false;
    }
};

But the nodeName is every time a DIV. In this DIV I have an JQGrid where are the textboxes in.

<div id="divGrdPos" style="padding:3px,0px,3px,0px;">
    <table id="JQGridCart" class="grdCart"></table>
</div>

Can someone help me?

like image 957
Senni Avatar asked Sep 03 '25 16:09

Senni


1 Answers

You need something like:

document.addEventListener('contextmenu', function (event) {
    if (event.target.nodeName !== 'INPUT' && event.target.type !== 'text' && event.target.nodeName !== 'TEXTAREA') {
        event.preventDefault();
    }
});

It will work for inputs (type="text") and textareas.

Jsfiddle: http://jsfiddle.net/QjmHy/

like image 57
micnic Avatar answered Sep 05 '25 06:09

micnic