Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created September 18, 2015 09:51
Show Gist options
  • Select an option

  • Save egulhan/d4d880d41d040045a3e3 to your computer and use it in GitHub Desktop.

Select an option

Save egulhan/d4d880d41d040045a3e3 to your computer and use it in GitHub Desktop.
How-to select text of element using javascript
/**
* Select text of element by element id
* @param element
*/
function selectText(elementId)
{
var
doc = document,
text = doc.getElementById(elementId),
range,
selection;
if (doc.body.createTextRange)
{
range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
}
else if (window.getSelection)
{
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment