How can I copy a simple string (for instance "foobar") to clipboard using JavaScript?
(jQuery is also accepted, since I already use it)
I've searched half an hour and still haven't found an easy solution, I can't believe it's so hard.
How can I copy a simple string (for instance "foobar") to clipboard using JavaScript?
(jQuery is also accepted, since I already use it)
I've searched half an hour and still haven't found an easy solution, I can't believe it's so hard.
Here you have:
function copyText(text) {
var $input = $("<input>");
$("body").append($input);
$input.val(text).select();
document.execCommand("copy");
$input.remove();
}
// Usage:
copyText("try!");
you can try this by pasting it in dev console and you'll have the text in your clipboard.