solarua
solarua

Reputation: 93

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.

Upvotes: 9

Views: 1817

Answers (1)

Tim Down
Tim Down

Reputation: 324627

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

Upvotes: 9

Related Questions