[whatwg] Request for new DOM property textarea.selectionText

Biju bijumaillist at gmail.com
Tue May 11 20:47:42 PDT 2010

On Mon, May 10, 2010 at 11:50 AM, Daniel Glazman
<daniel.glazman at disruptive-innovations.com> wrote:
> Le 10/05/10 07:11, Biju a écrit :
>> Can we have a new DOM property textarea.selectionText
>> which can be used to set/get selection text of a TEXTAREA/INPUT type=text.
>> Current method is complicated and varies between IE and Firefox
> Oh!!! As a read/write property, right?

But Ojan suggested another option, I liked that too.

Both have advantages

Option 1 (textobj.selectionText) advantages
* No need for web developer to provide selection start/end
* Automatically select the newly inserted text,
   (as most time that is what you want)
* No need for web developer to calculate selection start/end for
setting it after text insert
* preserve scroll
* Has both Getter and Setter

Option 2 (textobj.setRangeText(start, end, text)); advantages
* Independent of selection, web developer can change text at any range
   with out affecting selection
* Web developer has more options with it.
* preserve scroll

So I want to merge both suggestion, with a new signature,
also trying to reduce coding for web developer "less code less bug"

textobj.setRangeText(newtext, start, end, cursorpos)

 - optional parameter
 - new text that will replace existing
 - if missing/null/undefined/NaN then default to ""

 - optional parameter
 - starting position of the original textobj text that need to be replaced
 - if missing/null/undefined/NaN then default to textobj.selectionStart
 - negative value make start position from last character in the text content

 - optional parameter
 - ending position of the original textobj text that need to be replaced
 - if missing/null/undefined/NaN then default to textobj.selectionEnd
 - negative value make end position from last character

 - optional parameter
 - if missing/null/undefined/NaN then default to 0
 - what should happen to cursor/selection after text insert, its values are
 - - 0 - select the newly inserted text
 - - 1 - place cursor at beginning of inserted text
 - - 2 - place cursor at the end of inserted text
 - - 3 - keep selection unaffected
* issue, when value is 3 what should we do when setRangeText is replacing text
which has some parts selected and some other parts unselected

so if somebody want replace selection text it is just
which is almost same as my original proposal, and
will blank a selection

We could also add a getter method, also with optional parameters
     textobj.getRangeText(start, end)
textobj.getRangeText() gives current selection
textobj.getRangeText(0) gives from start of text to current selection end
textobj.getRangeText(null, -1) gives from current selection start to end of text

More information about the whatwg mailing list