3

I have an element(textArea). Now I would like a long press event and a double click event on the element. I am able to do this but I would also like to use event.preventDefault() in the mousedown event of long press event. This in turn prevents the dblClick event also.

The reason why I want to preventDefault is I am rendering an element on longPress and wanted to prevent the initial mouseDown as I am firing mousemove after longpress. I have searched and re-searched the net but am unable to find a good answer which solves the problem of long press and dblclick on the same element.

thanks!!

theRev
  • 109
  • 1
  • 2
  • 12

1 Answers1

4

try this Demo

HTML

<input type="button" ondblclick="whateverFunc()" onmousedown="func(event)" onmouseup="revert()" value="hold for long"/>

JavaScript

var timer;
var istrue = false;
var delay = 3000; // how much long u have to hold click in MS
function func(e)
{
   istrue = true;
   timer = setTimeout(function(){ makeChange();},delay);
  // Incase if you want to prevent Default functionality on mouse down
  if (e.preventDefault) 
  { 
     e.preventDefault();
  } else {
     e.returnValue = false; 
  }
}

function makeChange()
{
      if(timer)
      clearTimeout(timer);

      if(istrue)
      {
            /// rest of your code
          alert('holding');

      }
}
function revert()
{
   istrue =false;
}
function whateverFunc()
{
    alert('dblclick');
}
Voonic
  • 4,667
  • 3
  • 27
  • 58
  • can we somehow do this using eventListeners or by using events inside script. As I wanted to prevent the default mousedown action on long click. Thanks!!! – theRev Oct 23 '13 at 12:15
  • thanks @shadow it worked, but I am not sure how it is doing, can you please explain?? I just wanted to know how the preventDefault part works, I was doing something similar but when I tried using preventDefault it used to prevent the doubleclick event also. – theRev Oct 23 '13 at 12:42
  • @theRev Hello Rev, preventDefault instructs browser that if there is a default behavior for this event on this object, then skip that default behavior. Can't discuss more in comments. However if you really want to know, then do some research on how javascript recognizes clicks,dblclicks and other events. You will learn many things. Good Luck :-) – Voonic Oct 23 '13 at 13:10