Skip to content

Instantly share code, notes, and snippets.

@hypomodern
Created May 28, 2010 15:03
Show Gist options
  • Save hypomodern/417264 to your computer and use it in GitHub Desktop.
Save hypomodern/417264 to your computer and use it in GitHub Desktop.
(function($) {
$.fn.selectText = function() {
var doc = window.document;
return this.each(function() {
var selection, range;
if (window.getSelection && doc.createRange) {
// modern FF, Safari, and Opera (I think)
selection = window.getSelection();
range = doc.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();
selection.addRange(range);
} else if (doc.body.createTextRange) {
// guess who?
range = doc.body.createTextRange();
range.moveToElementText(this);
range.select();
}
});
}
})(jQuery);
jQuery(".nice_codes").live("click", function() { jQuery(this).selectText(); });
@hypomodern
Copy link
Author

If you want to cause the entire text contents of an element to be selected, I give you $.fn.selectText. You might, say, use this on-click. Or to be annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment