Can't download or copy SVG without body element
How do I successfully copy to clipboard the SVG content on this page?
I get an error at the select()
method that looks like this:
Uncaught TypeError: el.select is not a function at <anonymous>:1:4
This is my code at the moment that can be run in the console.
function copyClip() { const docEl = document.documentElement const string = new XMLSerializer().serializeToString(docEl) const el = document.createElement('textarea') docEl.insertAdjacentElement('beforeend', el) el.value = string el.select() document.execCommand('copy') } copyClip()
One answer to this is to use a different execution of copying to clipboard but I don’t understand why the select
method in the original question isn’t working. This function works:
const docEl = document.documentElement const string = new XMLSerializer().serializeToString(docEl) navigator.clipboard.writeText(string).then( function () { alert('The SVG was copied to your clipboard.') }, function (err) { alert('Could not copy:', err) } )