Copy the text to clipboard
I got a variable value from the server. Originally it was from alter window so it is hard to copy/paste the text.
So I use the following code to retrieve it.
- function copyToClipboard(textToCopy){
- $("body")
- .append($('<input type="text" name="fname" class="textToCopyInput"/>' )
- .val(textToCopy))
- .find(".textToCopyInput")
- .select();
- try {
- var successful = document.execCommand('copy');
- var msg = successful ? 'successful' : 'unsuccessful';
- alert('The password has been copied to clipboard!');
- } catch (err) {
- window.prompt("To copy the text to clipboard: Ctrl+C, Enter", textToCopy);
- }
- $(".textToCopyInput").remove();
- }
To use that I use the code
- var userKey = JSON.parse(response.responseText).UserKey;
- copyToClipboard(userKey);
After I Ctrl-V, I got the text on the clipboard was
copyToClipboard
So help me please.