Naeco
Naeco

Reputation: 133

Detect a text selection on a smartphone

On Android Chrome, when selecting a text by long press, the events "touchend" and "touchstart" are not triggered. The following solutions therefore do not work:

How to detect a long touch pressure with javascript for android and iphone?

Which event is triggered?

The "onselect" event seems to work only in inputs or textarea.

Upvotes: 1

Views: 2258

Answers (2)

Naeco
Naeco

Reputation: 133

It is not a "mobile event", it is a good old contextmenu event that is fired:

https://developer.mozilla.org/en/docs/Web/API/Element/contextmenu_event

I have finally found the answer here: Weird behavior of long presses in Chrome and Firefox for Android

Upvotes: 2

DevAb
DevAb

Reputation: 598

The "selectionchange" event is working for : Android (Chrome) and iOS (Safari).

The "contextmenu" event is working for : Android (Chrome, Mozilla and Opera).

For Mozilla and Opera you might need to wrap your function with a setTimeout:

document.addEventListener('contextmenu', setTimeout(handleSelection, 1));

Upvotes: 3

Related Questions