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
I added the touchstart event listener, but it does not fire on a touch event:
addEventListener("touchstart", function(event){
cl('touch fired');
}, true);
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.
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.
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.
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.
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);
Use touchstart and touchend events instead.
addEventListener("touchstart", function(event){
console.log('down fired');
event.target.classList.add('down');
}, true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With