Move Caret to End of Input or Textarea

By  on  

One of the annoying parts of using the focus method of HTML elements is that they don't move the cursor to the end of INPUT or TEXTAREA elements if they already have content in them.  That's probably the last thing a user would want.  I was browsing through Stack Overflow when I found this gem:  a function that moves the cursor to the end of an INPUT or TEXTAREA on command!

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

Simply pass the element to the function above and you'll see the caret move to the end of the element!  Caret management in the browser sucks, but this function makes it incredibly easy.  Enjoy!

Recent Features

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    MooTools Typewriter Effect Plugin

    Last week, I read an article in which the author created a typewriter effect using the jQuery JavaScript framework. I was impressed with the idea and execution of the code so I decided to port the effect to MooTools. After about an hour of coding...

  • By
    Do / Undo Functionality with MooTools

    We all know that do/undo functionality is a God send for word processing apps. I've used those terms so often that I think of JavaScript actions in terms of "do" an "undo." I've put together a proof of concept Do/Undo class with MooTools. The MooTools...

Discussion

  1. Brilliant piece of code..!! As always, I really enjoy your nifty scripts and this one made my day.
    Many Thanks David.

  2. Hello! Did you know you can also do a slight variation on the input.value = input.value; hack to make it work cross browser, something like:

    function focusAtEnd(el) {
      el.focus();
      var s = el.value;
      el.value = '';
      el.value = s;
    }
    
    • Thanks for commenting Chris! That will work but occasionally you see the “content flash” which is less than ideal.

  3. Agreed. Plus, I can’t imagine it’s very future proof relying on the behavioiur of the caret when setting the value. Your method is definitely the right way to do it.

  4. What if the user had clicked a very precise word to edit it ? Wont it be annoying to have the cursor at the end of the input when you clicked the beginning ? Maybe this method should only be used when the user is accessing a field with the tab key.

  5. Gergő Gyula

    Great snippet, just what I needed, thank you!!!

  6. Josef

    I don’t believe that this will work in Chrome on type=number inputs.

  7. Daniel Lidström

    Does it also scroll the text left so that the cursor is visible? This is a problem on iOS when the text is longer than the width of the input element.

  8. Sándor Hatvani

    Thank you.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!