Skip to content

Instantly share code, notes, and snippets.

@sinky
Created December 3, 2012 10:19
Show Gist options
  • Save sinky/4194061 to your computer and use it in GitHub Desktop.
Save sinky/4194061 to your computer and use it in GitHub Desktop.
jQuery Select Text
// Source: http://stackoverflow.com/a/987376/1461859 (http://jsfiddle.net/edelman/KcX6A/1506/)
jQuery.fn.selectText = function(){
var doc = document
, element = this[0]
, range, selection
;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};
$(function() {
$('p').click(function() {
$('#selectme').selectText();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment