synkeeks
synkeeks

Reputation: 23

Get the Highlight text in a specific div

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

Upvotes: 2

Views: 101

Answers (1)

Zword
Zword

Reputation: 6787

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;
}

Upvotes: 1

Related Questions