Solved

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

Posted on 2002-04-28
9
1,875 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 38
Javascript Regex 7 20
Accordion won't close correctly 5 14
Javascript: Range object 16 14
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

733 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