Get or set caret position, at where you can also insert text.
var inputor = document.getElementsByTagName('textarea')[0];
var caret = new Caret('inputor');
Get current caret position, return {start, end, text}:
caret.getPos();
Set current caret position:
caret.setPos({start: 1, end: 4});
Insert text at given postion:
carent.insert({start: 1, end: 4}, 'text');
Works fine both for textarea
and input
element.
All modern browser and IE5+
MIT license (© 2015 Roshan Wu)