How do I detect what object or ID or the user right-clicked on? I'm using the onContextMenu to trigger a function but I don't know how to detect the target.
Detectinig the target of the click using JavaScript?
3.8k Views Asked by Registered User At
3
There are 3 best solutions below
0

Your handler should accept an event object as its parameter; the srcElement property of the event will be the object that triggered the event.
0

As Patrick mentioned, you are receiving an event object as parameter to your onContentMenu callback function, where you can find the element triggered the event. I am using this code for cross-browser compatibility.
var oE = event.srcElement || event.originalTarget;
Note: originalTarget is Mozilla specific. You might want to pay attention to event.target https://developer.mozilla.org/en/DOM/event.target
PS. I've seen similar code before ;)