In my react
project I try to copy a text to clipboard
. In the function, I try to create a hidden input, select it and exec
"copy" in the document object as seen below:
const copyToClipboard = (textToCopy) => {
console.log('textToCopy', textToCopy); // Outputs
let input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "temp_copy_input")
input.setAttribute("value", textToCopy);
input.select()
console.log('input', input) // outputs object successfully
document.execCommand("copy");
input.remove();
}
All console.log
work fine, but it doesn't seem to copy the input value.
What am I missing?
Note: I think it's not a duplicate. There's a little detail I missed here, appending the input to the document.