Can't download or copy SVG without body element

How do I successfully copy to clipboard the SVG content on this page?

https://cdn.dribbble.com/assets/dribbble-ball-icon-e94956d5f010d19607348176b0ae90def55d61871a43cb4bcb6d771d8d235471.svg

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()  
Add Comment
1 Answer(s)

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)       }     ) 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.