A JavaScript selection script

I wanted a lightweight, cross-browser plugin to be able to handle selections in a text area (for my griffin.editor plugin). I didn’t find a suitable one despite asking on stackoverflow.com, so I created my own. It’s standalone and only 2.6kb (uncompressed).

Usage:

//jQuery is not required but supported.
var selection = new TextSelector($(‘#mytextarea’));
selection.replace(‘New text’);

// you can change selection:
selection.select(1,10); // select char 1 to 10

// get selection information
console.log("Start char: " + selection.get().start);

// check if anything is selected
selection.isSelected();

// get the text
var text = selection.text();

Code at github.