Skip to content

Instantly share code, notes, and snippets.

@jonpemby
Last active February 2, 2018 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonpemby/522b389cd0e600bb396b431052cb27f5 to your computer and use it in GitHub Desktop.
Save jonpemby/522b389cd0e600bb396b431052cb27f5 to your computer and use it in GitHub Desktop.
Select end of text in a contenteditable.
/**
* 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);
};
/**
* 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