How to trigger a JavaScript function when someone selects a given text fragment on a page using mouse?
Also, is there any way to find the position of selected text on the page?
Update:
To be more clear, text fragment can be part of a sentence or a word or a phrase or whole a paragraph.
Update:
In the meantime, a "Text was selected" (DOM) event was created and is supported by all current browsers: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select_event
Note this event only works on form elements, i.e. added to the
HTMLInputElementAPIThere is no "Text was selected"
(DOM)event, but you can bind amouseupevent to thedocument.body. Within that event handler, you might just check theor
methods. There are several topics on Stackoverflow, like this one javascript to get paragraph of selected text in web page.
I’m not sure what you mean with "finding the position", but to stay in my example world you could use the
event propertysfor X+Y mouse positions.Example: http://www.jsfiddle.net/2C6fB/1/