Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Could somebody please help me find out how to have a right click event captured for the
tag.

Lets say, I have a function named function1(), which i want to call on the right click on the div tag.

Is there any way out?
Posted

1 solution

Take a look at this: Javascript - Event properties[^].
Value for right mouse button is 2, although note that it recommends using mousedown or mouseup events rather than click.

Here is a sample from the page showing right click detection:
JavaScript
function doSomething(e) { 
    var rightclick; 
    if (!e) var e = window.event; 
    if (e.which) rightclick = (e.which == 3); 
    else if (e.button) rightclick = (e.button == 2); 
    alert('Rightclick: ' + rightclick); // true or false 
} 


Attach method like this to div and inplace of alert call your function1()
 
Share this answer
 
Comments
sargamlucy 6-Jul-10 5:14am    
Hi Sandeep,

Thanks for responding.

Actually i had seen this function somewhere, but had the following doubts.

Do i need to call this function for the "OnClick" event of div? And what should i pass for "e" in the function doSomething(e)?

Regards,
Snigdha
Sandeep Mewara 6-Jul-10 5:33am    
Try writing :
document.onclick = doSomething; above function line.

This would put a click event to entire document. If you want this on just a div then instead of document use the div object.

Run your application and check.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900