How can you move the cursor to the last position of a textarea in Javascript?

xgMz’s answer was best for me. You don’t need to worry about the browser:

var html = $("#MyTextArea").val();
$("#MyTextArea").focus().val("").val(html);

And here’s a quick jQuery extension I wrote to do this for me next time:

; (function($) {
    $.fn.focusToEnd = function() {
        return this.each(function() {
            var v = $(this).val();
            $(this).focus().val("").val(v);
        });
    };
})(jQuery);

Use like this:

$("#MyTextArea").focusToEnd();

Leave a Comment