简体   繁体   中英

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.

Note that I know nothing about JavaScript, so be gentle.

Thanks

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"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM