문제

I have the following fiddle: http://jsfiddle.net/sxAss/

By using the solution posted here I was able to move the caret to the end of the input. The problem that I have is that the input does not refresh to actually show the caret. Instead it shows the beginning of the input. What should I do to actually move the view to the end of the input. I am using Chrome.

function moveCaretToEnd(el) {
    if (typeof el.selectionStart == "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();
    }
}
도움이 되었습니까?

해결책

First focus your input element then call your moveCaretToEnd function

like

$('#button').click(function() {
    $('#input').focus();
    $('#input').val($('#input').val() + 'StackOverflowTest');
    moveCaretToEnd(document.getElementById('input'));
});

Working fiddle

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top