Created
September 18, 2015 09:51
-
-
Save egulhan/d4d880d41d040045a3e3 to your computer and use it in GitHub Desktop.
How-to select text of element using javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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