Skip to content

Instantly share code, notes, and snippets.

@pherrymason
Created December 15, 2013 09:33
Show Gist options
  • Select an option

  • Save pherrymason/7970814 to your computer and use it in GitHub Desktop.

Select an option

Save pherrymason/7970814 to your computer and use it in GitHub Desktop.
Select text in a node.
function SelectText(element) {
var doc = document;
var text = doc.getElementById(element);
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
$(function() {
$('p').click(function() {
SelectText('selectme');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment