Skip to content

Instantly share code, notes, and snippets.

@rblakejohnson
Created January 10, 2018 21:45
Show Gist options
  • Save rblakejohnson/0833177c721788b80271bf42d2c54ae2 to your computer and use it in GitHub Desktop.
Save rblakejohnson/0833177c721788b80271bf42d2c54ae2 to your computer and use it in GitHub Desktop.
Set cursor to the end of Contenteditable
function setEndOfContenteditable(contentEditableElement) {
var range = document.createRange();//Create a range (a range is a like the selection but invisible)
var selection = window.getSelection();//get the selection object (allows you to change selection)
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
// found here https://stackoverflow.com/posts/3866442/revisions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment