Advertisement
Guest User

Jenne van der Meer

a guest
Jun 15th, 2010
1,534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.36 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <style> .red { color:red; } </style>
  4.         <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  5.         <script>
  6.             // as found on http://gist.github.com/399624 (June 16th 2010)
  7.             jQuery.fn.single_double_click = function(single_click_callback, double_click_callback, timeout) {
  8.                 return this.each(function(){
  9.                     var clicks = 0, self = this;
  10.                     jQuery(this).click(function(event){
  11.                         clicks++;
  12.                         if (clicks == 1) {
  13.                             setTimeout(function(){
  14.                                 if(clicks == 1) {
  15.                                     single_click_callback.call(self, event);
  16.                                 } else {
  17.                                     double_click_callback.call(self, event);
  18.                                 }
  19.                                 clicks = 0;
  20.                                 $('#debug').html(clicks);
  21.                             }, timeout || 300);
  22.                         }
  23.  
  24.                         $('#debug').html(clicks);
  25.                     });
  26.                 });
  27.             }
  28.  
  29.             $(document).ready(function(){
  30.                 // this works as espected in FF and IE8
  31.                 // $('input').dblclick(function(){$('#toggle').toggleClass('red');});
  32.  
  33.                 // this misses alot of clicks in IE8 (only one I have)
  34.                 $('input').single_double_click(
  35.                     function(){ $('#toggle').toggleClass('red'); }, // toggle class on single click
  36.                     function(){ $('#toggle').toggle(); } // toggle visibility
  37.                 );
  38.             });
  39.         </script>
  40.     </head>
  41.     <body>
  42.         <div id="debug">0</div>
  43.  
  44.         <input type="button" value="Click me!" />
  45.         <div id="toggle">Testing double clicks!</div>
  46.     </body>
  47. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement