To copy data to the clipboard using JavaScript, you can use the following code:
index.tsx229 chars9 lines
This code creates a new textarea
element, sets its value to the text
parameter, adds it to the document body, selects its contents, copies them to the clipboard using the execCommand
method, and removes the textarea
element from the document.
To use this function, simply call it with the text you want to copy:
index.tsx34 chars2 lines
Note that in modern browsers, the execCommand
method may only work in response to a user-triggered event, such as a click or key press. To work around this restriction, you can listen for a copy
or paste
event and trigger the copying or pasting code in response to it.
gistlibby LogSnag