Created
December 16, 2021 04:14
-
-
Save fysherman/705474e1525c994188e031d98274fc9b to your computer and use it in GitHub Desktop.
Set caret end of contenteditable element
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
function setCaretEndOfElement(element) { | |
if(document.createRange === undefined || window.getSelection === undefined) return; | |
const range = document.createRange(); | |
const selection = window.getSelection(); | |
range.selectNodeContents(element); | |
range.collapse(false); | |
selection!.removeAllRanges(); | |
selection!.addRange(range); | |
element.focus(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment