You can do this in browsers that support extend()
( MDN ) for Selection
objects. Support for Mozilla, WebKit and Opera; IE does not support and does not include version 11. extend()
was added to the HTML Editing API specification , so it can 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); } } }
source share