Created
August 15, 2013 14:52
-
-
Save raphaelchaib/6241453 to your computer and use it in GitHub Desktop.
Javascript: Select text inside element (can be used with hover, focus, etc)
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
| 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