W3C home > Mailing lists > Public > whatwg@whatwg.org > August 2004

[whatwg] Standard method to get/set caret position

From: Neil Deakin <enndeakin@sympatico.ca>
Date: Sun, 29 Aug 2004 17:50:54 -0400
Message-ID: <41324FBE.6010203@sympatico.ca>

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

>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 
>viable solution?
>
>Greg
>
>
>
>
>  
>
Received on Sunday, 29 August 2004 14:50:54 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:58:36 UTC