Indeed, according to the documents ,
If the target element is removed from the document, events will still be aimed at it, and therefore, it is not necessary to bubble up to the window or document anymore. If there is a risk of removing an element while it is concerned, it is best to attach the listeners' touch directly to the target.
It turns out that the solution is to attach touchmove and touchend listeners to event.target itself, for example:
element.addEventListener("touchstart", (event) => { const onTouchMove = () => { // handle touchmove here } const onTouchEnd = () => { event.target.removeEventListener("touchmove", onTouchMove); event.target.removeEventListener("touchend", onTouchEnd); // handle touchend here } event.target.addEventListener("touchmove", onTouchMove); event.target.addEventListener("touchend", onTouchEnd); // handle touchstart here });
Even if the event.target element is removed from the DOM, events will continue to fire normally and give the correct coordinates.
Zitro source share