Created
May 28, 2014 11:33
-
-
Save joews/de5bc4da913899b31636 to your computer and use it in GitHub Desktop.
jQuery plugin to select contents of a 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
// 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