Skip to content

Instantly share code, notes, and snippets.

@raulb
Created May 18, 2012 21:11
Show Gist options
  • Save raulb/2727627 to your computer and use it in GitHub Desktop.
Save raulb/2727627 to your computer and use it in GitHub Desktop.
Cross browser select text
function selectText(element) {
var text = document.getElementById(element);
if ($.browser.msie) {
var range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if ($.browser.mozilla || $.browser.opera) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
} else if ($.browser.safari) {
var selection = window.getSelection();
selection.setBaseAndExtent(text, 0, text, 9999);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment