-
-
Save jp26jp/cdf3bfe4a80f11013bf82f909d0dbe85 to your computer and use it in GitHub Desktop.
Select all text
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
| $('pre code').on('mouseup', function() { | |
| var sel, range; | |
| var el = $(this)[0]; | |
| if (window.getSelection && document.createRange) { //Browser compatibility | |
| sel = window.getSelection(); | |
| if(sel.toString() == ''){ //no text selection | |
| window.setTimeout(function(){ | |
| range = document.createRange(); //range object | |
| range.selectNodeContents(el); //sets Range | |
| sel.removeAllRanges(); //remove all ranges from selection | |
| sel.addRange(range);//add Range to a Selection. | |
| },1); | |
| } | |
| }else if (document.selection) { //older ie | |
| sel = document.selection.createRange(); | |
| if(sel.text == ''){ //no text selection | |
| range = document.body.createTextRange();//Creates TextRange object | |
| range.moveToElementText(el);//sets Range | |
| range.select(); //make selection. | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment