views:

84

answers:

4

It may sound a newbie question but I wanted to know how I can trigger a custom 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 webpage?

Update: To be more clear, text fragment can be part of a sentence or a word or a phrase or whole a paragraph.

+1  A: 

There is no "Text was selected" (DOM) event, but you can bind a mouseup event to the document.body. Within that event handler, you might just check the

document.selection.createRange().text

or

window.getSelection()

methods. There are several topics on Stackoverflow, like this one http://stackoverflow.com/questions/845390/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 propertys for X+Y mouse positions.

Example: http://www.jsfiddle.net/2C6fB/1/

jAndy
Thanks Andy, Ya I think best we can get is X+Y mouse positions.
abhishektiwari
You should also consider selection via the keyboard.
Tim Down
Thanks for comments Tim. any pointers to selection via the keyboard?
abhishektiwari
+1  A: 

Checkout this thread

http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse

tummy.developer
Thanks tummy that is very interesting thread
abhishektiwari
+6  A: 

Here's a quick mashup:

$('div').mouseup(function() {
    alert(getSelectedText());
});

function getSelectedText() {
    if (window.getSelection) {
        return window.getSelection().toString();
    } else if (document.selection) {
        return document.selection.createRange().text;
    }
    return '';
}​

<div>Here is some text</div>

Demo: http://jsfiddle.net/FvnPS/11/

karim79
thanks Karim, it was helpful
abhishektiwari
There seems to be a pervasive idea that `window.getSelection();` is equivalent to IE's `document.selection.createRange().text;`. Are people copying from the same, inaccurate source? Anyway, `window.getSelection()` returns a `Selection` object while `document.selection.createRange().text;` returns a string, which is a very different object. The confusion arises from the fact that the `toString` method of `Selection` object returns the selected text, meaning that `alert(window.getSelection());` will alert the selected text.
Tim Down
Fixed. The mozilla docs say: "This makes the selection object appear like a string, when it is really an object with its own properties and methods. Specifically, the return value of calling the toString() method of the Selection object is passed." :) https://developer.mozilla.org/en/window.getSelection
karim79
Heh. I hadn't seen that before. Sorry to whinge on your answer: as you may have inferred, I've corrected this a few times on SO before.
Tim Down
@Tim Down - You weren't winging, you were in fact correct and did the right thing in pointing that out.
karim79
+1  A: 

AFAIK, there is no such event you described. But you can emulate that function.

Look over here for the code and demo.

ShiVik
Thanks ShiVik, it is really helpful. Does this emulation has browser dependence?
abhishektiwari
@abhishiktiwari - AFAIU the blog - none
ShiVik