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:
none
- selection is directionless; neither start nor end is identified as anchor nor focus.forward
- selection start is anchor and selection end is focus.backward
- selection end is anchor and selection start is focus.
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.
- Previous: Using Web Fonts
- Next: Year One