Skip to content

Instantly share code, notes, and snippets.

@joews
Created May 28, 2014 11:33
Show Gist options
  • Save joews/de5bc4da913899b31636 to your computer and use it in GitHub Desktop.
Save joews/de5bc4da913899b31636 to your computer and use it in GitHub Desktop.
jQuery plugin to select contents of a contenteditable element
// Select text of a content editable
// http://stackoverflow.com/a/6150060/2806996
// Example usage: $('[contenteditable]:first').selectText()
$.fn.selectText = function() {
var range = document.createRange();
range.selectNodeContents(this[0]);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
this.focus();
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment