Last active
February 2, 2018 17:15
-
-
Save jonpemby/522b389cd0e600bb396b431052cb27f5 to your computer and use it in GitHub Desktop.
Select end of text in a contenteditable.
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
/** | |
* Selects the end of the text in an element. Useful for contenteditable elements. | |
* @param {HTMLElement} target | |
* @return {void} | |
* | |
* note: This function does not support browsers < IE9 | |
*/ | |
export default (target) => { | |
const range = document.createRange(); | |
range.selectNodeContents(target); | |
range.collapse(false); | |
const selection = window.getSelection(); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
}; |
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
/** | |
* Selects the end of the text in an element. Useful for contenteditable elements. | |
* @param {HTMLElement} target | |
* @return {void} | |
* | |
* note: This function does not support browsers < IE9 | |
*/ | |
function selectEnd(target){ | |
var range = document.createRange(); | |
range.selectNodeContents(target); | |
range.collapse(false); | |
var selection = window.getSelection(); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment