[whatwg] Control over selection direction

Tim Down timdown at gmail.com
Fri Jan 14 03:09:59 PST 2011

On 14 January 2011 10:16, Marijn Haverbeke <marijnh at gmail.com> wrote:
> Another relevant precedent is window.getSelection().modify (Webkit and
> Gecko-2 specific), which uses the strings "forward" and "backward" to
> specify the direction in which to alter the selection. English is not
> my native language, and I'm not sure what the semantic difference
> between "forward" and "forwards" is, but I do expect people to
> misremember which one we end up using, and use the other one. Would it
> make sense to accept both the with-s and the without-s versions, or is
> that kind of do-what-mean stuff not HTML5-style?
> (This .modify method can, by the way, already be used, on the browsers
> that support it, to create reversed selections by setting a collapsed
> selection at the end of the desired selection, and then calling
> getSelection().modify("extend", "backward", "character") X times to
> adjust the start to the desired point. This is, unfortunately,
> horribly slow, and quite clunky.)

If you don't need the TextRange-like character-based modification, you
can instead use the selection's extend() method (supported in Mozilla,
WebKit and Opera for years) to create a backwards selection:

function selectRangeBackwards(range) {
    var sel = window.getSelection();
    var endRange = range.cloneRange();
    sel.extend(range.startContainer, range.startOffset);


More information about the whatwg mailing list