[whatwg] Standard method to get/set caret position

Ian Hickson ian at hixie.ch
Sun Aug 29 09:44:56 PDT 2004

On Thu, 26 Aug 2004, Ted Mielczarek wrote:
> While working on a javscript tool, I needed a way to get and set the 
> caret position in a textbox.

Interesting. Just out of curiosity, what is it that you were doing that 
required access to the caret position? If I may ask.

>     // for IE
>     if(inp.createTextRange)
>     {
>       var docrange = document.selection.createRange();
>       var inprange = inp.createTextRange();
>       inprange.setEndPoint('EndToStart', docrange);
>       return inprange.text.length;
>     }

I was going to add an IE-compatible version of this to the Web Forms spec 
but I honestly couldn't work out how it worked. I read almost every MSDN 
reference page on document.selection. textRange, and related APIs, and all 
I got out of it was a minor headache.

Sadly the Mozilla APIs you used in your example aren't perfect either -- 
the cursor could be at either selectionStart or selectionEnd, you don't 
really have a way of knowing.

DOM Range could be used to define the selection of the document but that 
wouldn't work for selections in input and textarea elements since those 
don't have DOM nodes they correspond to. We could define that they do but 
that's quite a big change.

Mozilla's window.getSelection() method doesn't actually return a DOM 
Range, it returns some custom interface.

So I don't know. What do people think? Should we try to be backards 
compatible with the Mozilla interface or the IE interface, or should we 
start from scratch here, and require that people write wrappers around 
the Moz and IE wrappers if they want compatibility? If we use something 
already defined, what do we use? What are the use cases that matter?

Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the whatwg mailing list