libcursor is a JavaScript library for manipulating cursor inside textareas and contenteditable html elements.
Using libcursor (Full API docs)
<script
type='text/javascript'
src='//sujeet.github.io/libcursor/libcursor.js'>
</script>
/**
* Write "Hello world!" with cursor
* ending up just after the 'o' in 'Hello'
*/
Cursor
.current()
.insert("world!")
.moveBackward(6)
.insert("Hello")
.insertAfter(" ");