Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch Screen and Javascript DOM Mousedown Event

Tags:

javascript

dom

I have a javascript web application that uses a touchscreen, browser is webkit based.

I am having a problem with this:

addEventListener("mousedown", function(event){  
    console.log('down fired');
    event.target.classList.add('down');
}, true);

When using a mouse, the target element class is added immediately when the mouse is held down, but when using the touchscreen, the target element class is not changed when the finger is held on the element.

The strange thing is however, the console log message is sent on the down event for both the mouse click and the ts press.

Any suggestions on how to solve this ??

Thanks

EDIT

I added the touchstart event listener, but it does not fire on a touch event:

addEventListener("touchstart", function(event){ 
    cl('touch fired');
}, true);
like image 275
crankshaft Avatar asked Feb 15 '13 02:02

crankshaft


People also ask

Does Mousedown work for touch?

The equivalent for onmousedown on touchscreen devices is ontouchstart , and the equivalent of onmouseup is ontouchend . If you would also like to detect dragging, you could use ontouchmove which is fired every time you move your finger after touching the screen. Note this will fire twice if you use like element.

What is Mousedown event in Javascript?

Definition and Usage. The mousedown event occurs when the left mouse button is pressed down over the selected element. The mousedown() method triggers the mousedown event, or attaches a function to run when a mousedown event occurs. Tip: This method is often used together with the mouseup() method.

How do I use Mousedown event?

To cause a MouseDown event for a form to occur, press the mouse button in a blank area or record selector on the form. To cause a MouseDown event for a form section to occur, press the mouse button in a blank area of the form section.

Is Mousedown the same as click?

Note: This differs from the click event in that click is fired after a full click action occurs; that is, the mouse button is pressed and released while the pointer remains inside the same element. mousedown is fired the moment the button is initially pressed.


2 Answers

Way too late, but maybe someone else could use it:

event.target doesn't work on touchscreen, because you could use more than 1 finger, so there are more targets:

addEventListener("mousedown", function(event){  
  console.log('down fired');
  var t = /touch/.test(event.type) ? event.targetTouches[0] : event.target;
  t.classList.add('down');
}, true);
like image 144
Ro NL Avatar answered Nov 15 '22 22:11

Ro NL


Use touchstart and touchend events instead.

addEventListener("touchstart", function(event){  
    console.log('down fired');
    event.target.classList.add('down');
}, true);
like image 34
Code.Town Avatar answered Nov 15 '22 21:11

Code.Town