Categories
JavaScript Answers

How to detect long press in JavaScript?

Spread the love

Sometimes, we want to detect long press in JavaScript.

In this article, we’ll look at how to detect long press in JavaScript.

How to detect long press in JavaScript?

To detect long press in JavaScript, we can listen for the contextmenu event.

For instance, we write

document.addEventListener('contextmenu', callback);

to call addEventListener to listen for the contextmenu event.

The callback function is called when when the contextmenu event is triggered by long presses.

Conclusion

To detect long press in JavaScript, we can listen for the contextmenu event.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *