[whatwg] Standard method to get/set caret position
enndeakin at sympatico.ca
Sun Aug 29 14:50:54 PDT 2004
>I think the IE and Mozilla models are a bit complicated, and it would be
>relatively easy to create a wrapper for various UAs. Here's what I
>Given a text box or textarea that I'll refer to as "tx":
>tx.selectionStart: This is a read/write integer that would set or retrieve
> the starting index of the selected text (one-based, so "1" would be
> the first character in the string). If nothing is selected, 0 would
> be returned.
>tx.selectionEnd: This is a read/write integer that would set or retrieve
> the ending index of the selected text (one-based).
Mozilla uses this same properties except they are zero-based which is
more sensible. If nothing is selected, both have the same value (in
Mozilla, there is always a selection, though it may be collapsed to a
single point). Also, when specifying this you need to be clear that you
mean that the cursor should be before or after the character.
>tx.position: This is a read/write integer that would set or retrieve
> the position of the caret (again, one-based).
This should be given a more specific name such as cursorPosition or
caretPosition, so that it is clear what it's referring to. Otherwise,
someone might think it was a screen position.
>Given these three very simple ways to access the position of the caret and
>text selection, other derivatives available in IE's implementation
>(inputrange.text.length, for example) would be trivial to compute
>(tx.selectionEnd-tx.selectionStart). Given that the selectionStart and
>selectionEnd return 0 if nothing is selected, this would work properly in
>the case of length computations. Additionally, it would be easy to
>extract the text, given functions such as substring().
>This would cover everything I've wished for with cursor positioning
>within a text box or textarea and would be vastly simpler than what
>currently exists. What do others think - is this comprehensive? Is this a
More information about the whatwg