Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set Range object as "backwards selection"?

I create a Range object and then add this Range to selection

window.getSelection ().addRange(myRange);

How to set the selection direction? I mean direction which can be checked using the anchorNode, anchorOffset, focusNode and focusOffset properties of selections.

like image 418
solarua Avatar asked Oct 07 '11 23:10

solarua


1 Answers

You can do this on browsers that support the extend() (MDN) method of Selection objects. Mozilla, WebKit and Opera support it; IE does not up to and including version 11. extend() has been added to the HTML Editing APIs spec so it may yet appear in IE.

Here's an example function:

function selectRangeBackwards(range) {
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (typeof sel.extend != "undefined") {
            var endRange = range.cloneRange();
            endRange.collapse(false);
            sel.removeAllRanges();
            sel.addRange(endRange);
            sel.extend(range.startContainer, range.startOffset);
        }
    }
}
like image 57
Tim Down Avatar answered Nov 15 '22 14:11

Tim Down