Set cursor to specific position on specific line in a textarea
I've updated your fiddle see: http://jsfiddle.net/eghpf/2/
I've added var currentPosition
which is used in this method
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
Which comes from this jQuery Set Cursor Position in Text Area
What happens (is the reason why the cursor is always on the last position); is when you call $('#systemNotesbuilder').val(arrayOfLines.join("\n"));
the entire value gets overwritten with a new value placing the cursor after that new value. The call to set the cursor is (and should) be after that call.