4

How to select the text of the page in FireFox? For example, there's a paragraph of text, user select the text in those paragraph in a regular way.then, I want to know in which paragraph the text selected by user (in which position-xy coordinates, range position).

Cœur
  • 37,241
  • 25
  • 195
  • 267
user495688
  • 973
  • 2
  • 15
  • 25

4 Answers4

3

You've asked about selection coordinates twice before. I know I've given you a working answer, so why are you asking again?

Here's some code that will return you the innermost element containing the selection in Firefox (assuming a single selection; Firefox allows multiple selections). Hope it's helpful.

function getSelectionContainerElement() {
    var sel = window.getSelection(), el = null;
    if (sel.rangeCount) {
        var range = sel.getRangeAt(0);
        el = range.commonAncestorContainer;
        if (el.nodeType != 1) {
            el = el.parentNode;
        }
    }
    return el;
}
Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • yes, but I am confused to implement because the main problem is that I want to use the function to know the position of the selected text to locate an index of the array that I have. – user495688 Nov 11 '10 at 10:32
  • Position of the selected text relative to what? – Tim Down Nov 11 '10 at 12:48
  • relative with the following text, for example i have sentence like this **the harvest is the process of gathering mature crops from the fields** and the selected text is 'gathering'. then i want to know indeces of the following text (i meant the text between 'gathering') – user495688 Nov 11 '10 at 15:16
0

refer the getSelection(), to get an object which contains information about the selected text and its position in the parent Element

Simon
  • 3,509
  • 18
  • 21
0

Selection - MDC might help you to find answer of all your questions.

Chinmayee G
  • 7,947
  • 2
  • 31
  • 41
0

You can find the exact answer here

but this module is under construction, You can browse project and find this module.

Ata Iravani
  • 2,146
  • 7
  • 29
  • 40