Skip to content

Instantly share code, notes, and snippets.

@betawax
Created July 4, 2012 14:52
Show Gist options
  • Save betawax/3047769 to your computer and use it in GitHub Desktop.
Save betawax/3047769 to your computer and use it in GitHub Desktop.
Select text with JavaScript
$.fn.selectText = function() {
var element = $(this)[0];
if (document.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);
}
}
@betawax
Copy link
Author

betawax commented Jul 4, 2012

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