Skip to content

Instantly share code, notes, and snippets.

@raphaelchaib
Created August 15, 2013 14:52
Show Gist options
  • Select an option

  • Save raphaelchaib/6241453 to your computer and use it in GitHub Desktop.

Select an option

Save raphaelchaib/6241453 to your computer and use it in GitHub Desktop.
Javascript: Select text inside element (can be used with hover, focus, etc)
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, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment