Jun12

Using Both ondblclick & onclick Handlers

Back from hell after long I post a workaround for the ondblclick and onclick javascript issue: It’s not possible to define both handlers so that the onclick handler won’t get active on a doubleclick.

<input type="button"
           value="Example"
           onclick="MethodA();"
           ondblclick="MethodB();" />

A possible workaround is to use a semaphore-like global variable (here: dblclick). This is set to true whenever the doubleclick handler is performed.
Further we delay the single click handler for some miliseconds (here: 250) and check against the dblclick semaphore until continue.

var dblclick = false;
function MethodA()
{
     setTimeout(function()
     {
          if(dblclick == true)
               return;

          alert('MethodA');
     },250);
}
function MethodB()
{
     dblclick = true;
     alert('MethodB');
     setTimeout(function()
     {
          dblclick = false;
     },250);
}

Leave a Reply

You must be logged in to post a comment.