2

I've not found the answer of my question, so I come to you.

I need to retrieve the selected text in a particularly div when pushing on the F8 key on the keyboard.

Problem, I can get the selected text in the entire document (with window.getSelection()), but is there a way to retrieve the text if and only if the selected text is the div?

Thank you for your answer

synkeeks
  • 23
  • 2

1 Answers1

1

May be this may help you:

how to get selection inside a div using jquery/javascript

Using http://code.google.com/p/rangy :

function getSelectedTextWithin(el) {
    var selectedText = "";
    var sel = rangy.getSelection(), rangeCount = sel.rangeCount;
    var range = rangy.createRange();
    range.selectNodeContents(el);
    for (var i = 0; i < rangeCount; ++i) {
        selectedText += sel.getRangeAt(i).intersection(range);
    }
    range.detach();
    return selectedText;
}
Community
  • 1
  • 1
Zword
  • 6,605
  • 3
  • 27
  • 52