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.
<html>
<head>
<script type="text/javascript">
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
alert(e.target.nodeName); //or e.target.getAttribute('id')
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function(e) {
alert(window.event.srcElement.nodeName); //or e.srcElement and then like above
window.event.returnValue = false;
});
}
</script>
</head>
<body>
<span>Lorem ipsum...</span><br/>
body content
</body>
</html>
PS. I've seen similar code before ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With