-
-
Save ting-dev-coder/c2a7fec0c8b512335f6ca93d3657c592 to your computer and use it in GitHub Desktop.
select text inside of an element and then copy it to the clipboard
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 copy(obj) { | |
| try { | |
| if (obj) selectContent(obj) | |
| document.execCommand('copy') | |
| // clears the current selection | |
| window.getSelection().removeAllRanges() | |
| } catch (err) { | |
| console.log(err) | |
| } | |
| } |
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 selectContent(obj) { | |
| if (window.getSelection && document.createRange) { | |
| let sel = window.getSelection() | |
| let range = document.createRange() | |
| range.selectNodeContents(obj) | |
| sel.removeAllRanges() | |
| sel.addRange(range) | |
| } else if (document.selection && document.body.createTextRange) { | |
| let textRange = document.body.createTextRange() | |
| textRange.moveToElementText(obj) | |
| textRange.select() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment