I've found a couple resources for how to place the cursor in a textarea at the end of the text, but I can't sort out a simple way to make it appear at the beginning.
I'm prepopulating the textarea with some text and just want to make it easier on the users.
To set the cursor at the end of a textarea: Use the setSelectionRange() method to set the current text selection position to the end of the textarea. Call the focus() method on the textarea element. The focus method will move the cursor to the end of the element's value.
To position the cursor at the beginning of the contents of a TextBox control, call the Select method and specify the selection start position of 0, and a selection length of 0.
Setting the cursor position You can also hold down shift and then move the cursor with the arrow keys on the keyboard to select content. It has the same effect. The TinyMCE bookmarks this location with the Bookmark Manager API, and then loads the selected location later.
To move the cursor to the beginning of an input field:Use the setSelectionRange() method to move the cursor to the beginning of the input field. Call the focus() method on the input element. The focus method will move the cursor to the beginning of the element's value.
Pass a reference to your textarea to this JS function.
function resetCursor(txtElement) {
if (txtElement.setSelectionRange) {
txtElement.focus();
txtElement.setSelectionRange(0, 0);
} else if (txtElement.createTextRange) {
var range = txtElement.createTextRange();
range.moveStart('character', 0);
range.select();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With