views:

31

answers:

2

I'm looking for a way to set the text 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?

A: 

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);
}
Andy E
+1  A: 
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();
}
Tim Down