Created
March 20, 2018 22:17
-
-
Save arfeo/8bc289b8791d4e6cc2435fbed459da40 to your computer and use it in GitHub Desktop.
[ES6] Set caret to the end of text input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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