开发者

Setting marker position inside textarea

开发者 https://www.devze.com 2023-01-08 11:11 出处:网络
I\'m looking for a way to set the tex开发者_JAVA技巧t marker to the beginning of a textarea when there\'s a value set or text between the textarea tags. I couldn\'t find anything on it when searching.

I'm looking for a way to set the tex开发者_JAVA技巧t marker to the beginning of a textarea when there's a value set or text between the textarea tags. I couldn't find anything on it when searching. So, does anyone know how to go about doing this?


var el = document.getElementById("myTextArea"); 

if (typeof el.setSelectionRange != "undefined") {
    el.setSelectionRange(0, 0);
} else if (typeof el.createTextRange != "undefined") {
    var range = el.createTextRange();
    range.collapse(true);
    range.select();
}


The following should be something like what you're looking for, although I haven't tested it.

var el = document.getElementById("myTextArea"); 

// IE
if (document.selection) {
    var sel = el.createTextRange();
    sel.moveStart("character", 0);
} 
// Others
else if ("setSelectionRange" in el) {
    el.setSelectionRange(0, 0);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消