Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gubatron/6599366 to your computer and use it in GitHub Desktop.
Save gubatron/6599366 to your computer and use it in GitHub Desktop.
function selectText(element) {
var doc = document
, text = doc.getElementById(element)
, range, selection
;
if (doc.body.createTextRange) { //ms
range = doc.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) { //all others
selection = window.getSelection();
range = doc.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
@imubashirhashmi
Copy link

This is a great method for selecting text if you use a non-input element as a textbox. Thanks

@gubatron
Copy link
Author

wow, glad you found it and it still works, had totally forgotten about this gist.

Cheers!

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