By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Click event and Double Click event on a single button

P: 1
Hello Friends,
I want to trigger both the click event and double click event in a single button separately. (i.e.) the user can trigger both the single click and the double click event. if they click once, it should trigger single click event. if they click twice, it should trigger double click event.

please Help.


Cumars
Oct 11 '06 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
Add both onclick and ondblclick event handlers to the button object, e.g.
[html]<input type="button" onclick="alert('you only clicked once');" ondblclick="alert('you double clicked');" value="Single or Double Click">[/html]
May 22 '08 #2

100+
P: 428
You can avoid the single click handler when you doubleclick,
but it requires setting a timeout for the single click handler,
and cancelling it if a doubleclick is called on.

Expand|Select|Wrap|Line Numbers
  1. function oneclickevent(e){
  2.     e= window.event || e;
  3.     var who= e.srcElement || e.target;
  4.     var s= who.id || who.tagName;
  5.     s+='\n'+e.type;
  6.     if(!window.clicktimer) window.clicktimer= setTimeout(function(){
  7.         alert(s);
  8.         window.clicktimer= undefined;
  9.     },
  10.     500);
  11. }
  12. function twoclickevent(e){
  13.     clearTimeout(clicktimer);
  14.     e= window.event || e;
  15.     var who= e.srcElement || e.target;
  16.     var s= who.id || who.tagName;
  17.     alert(s+'\n'+e.type);
  18.     window.clicktimer= undefined;
  19. }

var el= document.getElementById('mybutton');
el.onclick= oneclickevent;
el.ondblclick= twoclickevent;


The delay for the single click may be a pain,and difficult to set for all users.
And some people will click multiple times which can send a single click after the double click returns-
Besides, it is tough to implement for keyboard access-
consider making your second event a click+shiftkey,
and handle it without a delay either way.
May 22 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.