jldupont
jldupont

Reputation: 96716

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).

Upvotes: 3

Views: 390

Answers (2)

Rbacarin
Rbacarin

Reputation: 707

In jQuery you can use the select() function to bind an event handler to the select event. For more info, see http://api.jquery.com/select/

Upvotes: 0

Andrew
Andrew

Reputation: 1187

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.

Upvotes: 1

Related Questions