Quantcast
Viewing latest article 42
Browse Latest Browse All 43

Answer by Chase Seibert for How do I copy to the clipboard in JavaScript?

The other methods will copy plain text to the clipboard. To copy HTML (i.e., you can paste results into a WYSIWYG editor), you can do the following in Internet Explorer only. This is is fundamentally different from the other methods, as the browser actually visibly selects the content.

// Create an editable DIV and append the HTML content you want copiedvar editableDiv = document.createElement("div");with (editableDiv) {    contentEditable = true;}editableDiv.appendChild(someContentElement);// Select the editable content and copy it to the clipboardvar r = document.body.createTextRange();r.moveToElementText(editableDiv);r.select();r.execCommand("Copy");// Deselect, so the browser doesn't leave the element visibly selectedr.moveToElementText(someHiddenDiv);r.select();

Viewing latest article 42
Browse Latest Browse All 43

Trending Articles