On focus, how to place the caret after the last character in an already populated field in IE?
On focus, how to place the caret after the last character 开发者_Python百科 in an already populated field in IE?
textareaname.scrollTop = textareaname.scrollHeight;
Works for textareas
Nevermind, try this: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
IE9 makes it easy, with .selectionStart and .selectionEnd.
It looks like in prior versions you'll need to call .createTextRange, and then manipulate the returned object with .move, .moveStart, .moveEnd, and .movePoint.
The following will work in all major browsers, including IE 5-9. IE 9 will support the selectionStart
and selectionEnd
properties of text inputs, bringing it into line with all the other browsers.
function moveCaretToEnd(el) {
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
el.selectionStart = el.selectionEnd = el.value.length;
} else if (typeof el.createTextRange != "undefined") {
el.focus();
var range = el.createTextRange();
range.collapse(false);
range.select();
}
}
The easiest way is to reset content of input element, like this:
var element_content = false;
element = document.getElemetById('xyz');
//put focus on element and remember current content
element.focus();
element_content = element.value;
//now, clear content of element
element.value='';
//and return stored content back to the element, browser moves cursor to the end itself ;)
element.value=element_content;
精彩评论