Solved

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

Posted on 2002-04-28
9
1,868 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now