Skip to content

Instantly share code, notes, and snippets.

@cozzbie
Created November 30, 2018 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozzbie/641aa7d4b8a63fcc9ab67ab84a1d6c0b to your computer and use it in GitHub Desktop.
Save cozzbie/641aa7d4b8a63fcc9ab67ab84a1d6c0b to your computer and use it in GitHub Desktop.
const selection = window.getSelection();
const range = document.createRange();
const element = document.getElementById('elmt');
// Collect a range based off the contents of an element.
range.selectNodeContents(element);
// Clear ranges that might be stored in the selection object.
selection.removeAllRanges();
selection.addRange(range);
//Optional
document.execCommand('copy');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment