Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need cursor at beginning of text in textarea

I have this in my body and it works

onLoad='document.forms.post.message.focus()'

but I need the cursor to be placed in the textarea at the beginning of any existing text, not at the end. This puts it at the end.

like image 328
Beauford Avatar asked Jul 28 '10 19:07

Beauford


People also ask

How do I change the cursor position in textarea?

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.

How do I put a cursor in a text field?

Sometimes all you have to do to make sure the cursor is inside the text box is: click on the text box and when a menu is displayed, click on "Format text box" then click on the "text box" tab and finally modify all four margins (left, right, upper and bottom) by arrowing down until "0" appear on each margin.

How do you start a new text section at the current cursor position without adding a new page?

Select where you want a new section to begin. Go to Layout > Breaks, and then choose the type of section break you want. Next Page Starts the new section on the following page.

How do you insert text into the textarea at the current cursor position in react?

When you click a button you move the focused element so there is no cursor in the text anymore. So you would have to track the last location of the cursor and then just set the value of the text area to be the current value + the extra text at the location of the last know cursor position.


1 Answers

function moveCaretToStart(el) {
    if (typeof el.selectionStart == "number") {
        el.selectionStart = el.selectionEnd = 0;
    } else if (typeof el.createTextRange != "undefined") {
        el.focus();
        var range = el.createTextRange();
        range.collapse(true);
        range.select();
    }
}

moveCaretToStart(document.forms["post"].elements["message"]);
like image 128
Tim Down Avatar answered Sep 19 '22 06:09

Tim Down