Skip to main content

selectionDirection property

As of r92088 and r73510, WebKit and Mozilla respectively support selectionDirection property on input and textarea HTML elements. selectionDirection property lets web sites figure out which side of the selection is anchor and focus.

Traditionally, selection's anchor and focus were obtained through getSelection().anchorNode, getSelection().anchorOffset, getSelection().baseNode, and getSelection().baseOffset. However, these functions don't work with selections inside input and textarea like other methods and properties of Selection; input and textarea elements instead provide properties such as selectionStart and selectionEnd to let websites control selection within them. So we've added selectionDirection, which is a property on HTMLInputElement and HTMLTextAreaElement that takes the following values:

setSelectionRange now takes the direction as the 3rd argument so the following code will select "hello world":

<input type="text" value="hello world WebKit">
<script>
var text = document.getElementsByTagName('input')[0];
text.focus();
text.setSelectionRange(6, 11, 'backward');
window.getSelection().modify('extend', 'left', 'word');
alert(text.selectionDirection); // This will show 'backward';
</script>

You can learn more about selectionDirection property on HTML5 spec.