Preventing cursor position/scroll from moving after changing value in textarea

brettvienna

New Member
I'm trying to insert some text after a user presses enter in a textarea and position the cursor immediately after the inserted text. There's similar questions addressing the issue of setting the cursor to the desired position (http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area)\[code\]$('#input').on("keypress", function(e) { if (e.keyCode == 13) { e.preventDefault(); var value = http://stackoverflow.com/questions/15539247/$("#input").val(); pos = $("#input").prop('selectionStart'); $("#input").focus(); // following line changes the cursor position to the beginning $("#input").val(value.substring(0, pos) + "\nHello\n" + value.substring(pos)); this.setSelectionRange(pos + 7, pos+7); return false; }});\[/code\]
 
Back
Top