'How to prevent touchstart event while triggering click event
I just want click event handler to get executed and not the touchstart event handler (on mobile/touch devices), how can i prevent touchstart event handler from getting executed?
const container = document.getElementById("slider");
const btn = document.getElementById("btn");
container.addEventListener("touchstart", () => console.log("touchstart"));
btn.addEventListener("click", () => console.log("click"));
#slider {
width: 400px;
height: 400px;
display: flex;
justify-content: center;
align-items: center;
background-color: aquamarine;
margin: 100px auto;
border-radius: 10px;
}
#btn {
width: 40px;
height: 40px;
text-align: center;
line-height: 40px;
background-color: lightcoral;
padding: 30px;
border-radius: 10px;
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="slider">
<div id="btn">btn</div>
</div>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
