Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event on HTML selection

Is there an event for situations where something is selected on an HTML page like arbitrary text within a span element? Does such a thing exist or does one need to poll at regular interval & analyze the window.getSelection() ?

I would like to trap this event type from within a browser extension (Chrome).

like image 998
jldupont Avatar asked Feb 05 '26 16:02

jldupont


1 Answers

New York Times implements some "on selection" type of functionality on all their articles without monitoring for a specific "onselection" event. Example here, highlight some text and you'll see a "?" pop up that you can use for search.

Here's the relevant code. Basically looks like onmouseup of any body text, they get the document (or window) selection and use that.

The existing "onselect" event is spec'd to only apply to input and textarea elements.

like image 127
Andrew Avatar answered Feb 08 '26 07:02

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!