My specific use case is knowing what people have selected on the page and selecting text for them. This is for:
- FuncIt (the FuncUnit IDE we are developing)
- FuncUnit - I want FuncUnit to support highlighting text on a drag motion.
- One of our clients needs to allow people to select text in the page and perform various actions on it
In general, this might provide a good low-level API for building rich text editors. I've worked on 3 text editors. It was extremely painful. There's a lot of cross browser differences and even some outright bugs. For example, in IE, it adds extra characters around newlines in textareas.
Here's how selection works:
To select characters within an element:
$('#foo').selection(20, 100) -> Selects the 20th character to the 100th.
This works in inputs, textareas, and normal HTMLElements.
To get the current selection:
$('#foo').selection() -> {start: 20, end: 200, range: $.Range }
The nifty thing is this only gives to the selection within the element. If the selection goes outside of the dom element, it will return {start: 0, end: 220 } (assuming there are 220 characters in your element).
This isn't something I expect everyone to use. But I found myself wanting to know what the user has selected so many times I finally built something around it.