Skip to content

Instantly share code, notes, and snippets.

@jp26jp
Created November 23, 2016 03:42
Show Gist options
  • Select an option

  • Save jp26jp/cdf3bfe4a80f11013bf82f909d0dbe85 to your computer and use it in GitHub Desktop.

Select an option

Save jp26jp/cdf3bfe4a80f11013bf82f909d0dbe85 to your computer and use it in GitHub Desktop.
Select all text
$('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