Skip to content

Instantly share code, notes, and snippets.

@arfeo
Created March 20, 2018 22:17
Show Gist options
  • Save arfeo/8bc289b8791d4e6cc2435fbed459da40 to your computer and use it in GitHub Desktop.
Save arfeo/8bc289b8791d4e6cc2435fbed459da40 to your computer and use it in GitHub Desktop.
[ES6] Set caret to the end of text input
const setCaretToTheEnd = el => {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
const range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
const textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment