I am working on an in-browser editor within a textarea. I have started looking for some information on dealing with textarea selection and found this jQuery plugin, fieldSelection that does some simple manipulation.
However, it doesn’t explain what’s going on.
I want to understand more on textarea selection in JavaScript, preferably with a description of both pre-DOM3 and post-DOM30 scenarios.
Start with PPK’s introduction to ranges. Mozilla developer connection has info on W3C selections. Microsoft have their system documented on MSDN. Some more tricks can be found in the answers here.
In addition to incompatible interfaces you’ll be happy to know that there is extra bizarreness going on with
textareanodes. If I remember correctly they behave as any other nodes when you select them in IE, but in other browsers they have an independent selection range which is exposed via the.selectionEndand.selectionStartproperties on the node.Additionally, you should really take a look at
.contentEditableas a means to edit things live. From the release of Firefox3, this is now supported by all browsers.