Move Caret to End of Input or Textarea
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!
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
It's no secret that Facebook has become a major traffic driver for all types of websites. Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly. And of course there are Facebook "Like" and "Recommend" widgets on every website. One...
Although it's possible to achieve opacity using CSS, the hacks involved aren't pretty. If you're using the MooTools JavaScript library, opacity is as easy as using an element's "set" method. The following MooTools snippet takes every image with the "opacity" class and sets...
If you follow me on Twitter, you saw me rage about trying to make position: absolute
work within a TD
element or display: table-cell
element. Chrome? Check. Internet Explorer? Check. Firefox? Ugh, FML. I tinkered in the console...and cussed. I did some researched...and I...
Brilliant piece of code..!! As always, I really enjoy your nifty scripts and this one made my day.
Many Thanks David.
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:Thanks for commenting Chris! That will work but occasionally you see the “content flash” which is less than ideal.
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.
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.
Great snippet, just what I needed, thank you!!!
I don’t believe that this will work in Chrome on
type=number
inputs.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.
Thank you.