Skip to content

Instantly share code, notes, and snippets.

@dustinpoissant
Created February 7, 2017 17:06
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 dustinpoissant/16444234168d94a3d99a9c09c2606b22 to your computer and use it in GitHub Desktop.
Save dustinpoissant/16444234168d94a3d99a9c09c2606b22 to your computer and use it in GitHub Desktop.
Selects the text within an element.
jQuery.fn.selectText = function(){
var doc = document;
var element = this[0];
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};
jQuery.fn.selectText=function(){var a=document,b=this[0];if(a.body.createTextRange){var c=document.body.createTextRange();c.moveToElementText(b),c.select()}else if(window.getSelection){var d=window.getSelection(),c=document.createRange();c.selectNodeContents(b),d.removeAllRanges(),d.addRange(c)}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment