Grow your CSS skills. Land your dream job.

Last updated on:

Move Cursor to End of Input

Where el is a reference to an input or textarea.

function moveCursorToEnd(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();
    }
}

Reference URL

Comments

  1. Aqeel Bhatt
    Permalink to comment#

    I hope this solves the caret problem if I use <span contenteditable=”true”</span> instead of an <input type=”text”> :)

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".