Solved

How do i detect right click event for onClick()?

Posted on 2002-04-28
9
1,872 Views
Last Modified: 2012-05-05
i have javascript code as follow..

<script language=javascript>
function assignGroup(){
     alert(event.button);
}
</script>

<FONT SIZE=1.8 FACE =ARIAL COLOR=#006043 onClick=displayMenu()>My Text</FONT>

my question is,when user click on My Text, how do i know either the user is using right click or left click?

from the code above.. i get 0 for event.button when i left click at the text button get nothing when i right click on the text..please guide...

thanks
0
Comment
Question by:pleasure
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 6974696
try onMouseUp instead of onClick.

CJ
0
 
LVL 10

Expert Comment

by:eyal_mt
ID: 6974701
IE has the oncontextmenu event which only occurs for right clicks so you can use it with a different funtion.
0
 

Author Comment

by:pleasure
ID: 6974712
CJ:
onMouseUp didn't give any response...:(

eyal_mt:
can you please explain more about your method.. i'm quite new in JavaScript..:p


thanks...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 23

Expert Comment

by:b1xml2
ID: 6974713
<html>
<head>
<title>Click</title>
<script language="javascript">
function checkClick() {
 alert("Event Button - " + event.button + "\n" + (event.button == 1 ? "Main" : "Secondary") + " Button Clicked");
}
</script>
</head>
<body>
<font size="1.8" face="arial" color="#006043" onmouseup="checkClick()">my text</font>
<ol>
<li><b>Main Button Usually Refers To The Left Click And Returns A Value Of 1</b></li>
<li><b>Other Buttons Return Values Other Than 1</b></li>
</ol>
</body>
</html>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6974714
Works for MSIE4+
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6974722
as per CJ_S suggestion =). So, CJ_S should get credit for this.
0
 
LVL 10

Accepted Solution

by:
eyal_mt earned 50 total points
ID: 6974726
use:

<FONT SIZE=1.8 FACE =ARIAL COLOR=#006043 oncontextmenu=displayMenu()>My Text</FONT>

instead of onclick
your displayMenu function will only be called when the user right click on your tag.

to prevent the original popup menu from IE use:
event.returnValue=false;

inside your displayMenu function
0
 

Author Comment

by:pleasure
ID: 6975199
yupe.. oncontextmenu() works...:) thanks...

menuPopup = window.createPopup();
menuPopup.show(menuXPos, menuYPos, iWidth, iHeight, document.body);

i used code above to popup my menu, is there any way for me to set my background color of my popup menu? Currently it's showing with color.

0
 

Author Comment

by:pleasure
ID: 6976023
actually..both of the solution work..but since i'm using eyal_mt solution so i think it's better for me to reward him/her..thanks a lot to all of you...:)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question