DEV Community

Discussion on: 🎉 How to Copy an Image or a Text to Clipboard in Javascript (2022) ☘️

Collapse
 
gnowland profile image
Gifford Nowland

I'm having the same issue, did you find a solution?

Collapse
 
avillapalos profile image
Villapalos

Clipboard API write() is not compatible with Firefox, the only way to be able to copy an image is using the deprecated execCommand() function:

    const img = document.createElement('img')
    img.src = '<YOUR_IMAGE_SRC>'

    const div = document.createElement('div')
    div.contentEditable = 'true'
    div.appendChild(img)
    document.body.appendChild(div)
    this.selectElement(div)
    document.execCommand('copy')
    document.body.removeChild(div)
Enter fullscreen mode Exit fullscreen mode