I'm trying to create a simple function that takes the textContent
of a specific div
element and copies it to the clipboard.
But whenever I try to copy the text, I receive an error.
Am I missing something with Vue and his behavior with such actions?
HTML:
<div id="rgb">{{ RGB }}</div>
<button @click="copyColor('rgb')">Copy</button>
<div id="hex">{{ HEX }}</div>
<button @click="copyColor('hex')">Copy</button>
<div id="hsl">{{ HSL }}</div>
<button @click="copyColor('hsl')">Copy</button>
Javascript:
methods: {
copyColor(id){
var copyText = document.getElementById(id).textContent;
navigator.clipboard.writeText(copyText);
}
},
Errors:
[Vue warn]: Unhandled error during execution of native event handler
Uncaught TypeError: Cannot read properties of undefined (reading 'writeText')
Image of the error: https://i.stack.imgur.com/NQGmh.png
Thanks!