Skip to content

Instantly share code, notes, and snippets.

@GeoffCox
Created April 16, 2015 16:39
Show Gist options
  • Save GeoffCox/4b30910df4ea0d10e55a to your computer and use it in GitHub Desktop.
Save GeoffCox/4b30910df4ea0d10e55a to your computer and use it in GitHub Desktop.
Copy HTML to clipboard in any browser
// It may be worth making a copy of the element within <body> that is hidden
// or even producing a section of items to copy.
// This way you can remove any interactivity styles that make the copied content not
// look good when pasted into other applications.
var element = document.getElementById('ClipboardTemp');
// This technique is simulating the user selecting these elements.
// Think of a ControlRange as a selection of controls and then executing the command like the user pressing 'Ctrl+C'
element.contentEditable = 'true';
var controlRange;
if (document.body.createControlRange) {
controlRange = document.body.createControlRange();
controlRange.addElement(element);
controlRange.execCommand('Copy');
}
element.contentEditable = 'false';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment