I have a webpage. I need to do something on double-click event. However, it doesn't work…
- User
I have a webpage. I need to do something on double-click event. However, it doesn't work on Mobile device. What is the related event on mobile?
- Assistant
The related event on mobile devices is "touchstart" and "touchend" events.
- User
How to listen both events? They do the same thing.
- Assistant
You can use the
addEventListenermethod to listen to both events.Here is an example code that listens to both
touchstartandclickevents:codeelement.addEventListener('click', function() { // do something on click }); element.addEventListener('touchstart', function() { // do something on touchstart });In this code,
elementis the element you want to add the event to. This code will listen to bothclickandtouchstartevents. - User
Can combine together?
- Assistant
Yes, you can combine the
clickandtouchstartevents together by adding a single event listener that listens for both events using theaddEventListenermethod and passing in the event name separated by a space. Here is an example code:codeelement.addEventListener("click touchstart", function(event) { // Your code here });With this approach, the same function will be called when either of the events is triggered. The function can check which event has occurred using
event.type.