I’m trying to copy a json string to the clipborad:
export const copyToClipboard = () => {
const text = '{ "name": "hello"}';
const selBox = document.createElement('input');
selBox.style.position = 'fixed';
selBox.style.left = '0';
selBox.style.top = '0';
selBox.style.opacity = '0';
selBox.value = JSON.stringify(text);
console.log(text);
console.log(selBox.value);
document.body.appendChild(selBox);
selBox.select();
document.execCommand('copy');
document.body.removeChild(selBox);
};
The problem is, the value from selBox has the character in it.
The logs look like this:
{ "name": "hello"} This is the text
"{ "name": "hello"}" This is the value of selBox
Why is that happening and how do I fix it?
Advertisement
Answer
Variable text is already a string so there is no need for JSON.stringify()