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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With