[whatwg] Standard method to get/set caret position

Greg Kilwein gkilwein at fbsdata.com
Sun Aug 29 16:40:04 PDT 2004


On Sun, 29 Aug 2004, Neil Deakin wrote:
> >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
> >propose:
> >
> >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.

Ah, I see what you mean.  That makes sense and prefer the zero-based idea 
since there would always be a value for selectionStart and selectionEnd.  
The index would be the position before the character.  The index of the 
selection that would include the last character would be string.length.


> >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.

Great idea.  I prefer caretPosition and a zero-based indexing scheme to 
match selectionStart and selectionEnd.




More information about the whatwg mailing list