Jack Hales
Jack Hales

Reputation: 1644

Copy text to clipboard using JavaScript / PHP

Trying to make a link that will copy some text, and what I was wondering (I have read some articles but they were for when people input things) if there was a way to just put any random text into the clipboard? I wanted to do this using JavaScript to do the actual copying and use PHP to get the actual data that I want to copy, I dont know that much JavaScript so keep that in mind, Thanks.

Upvotes: 0

Views: 8559

Answers (2)

Hamza Awan
Hamza Awan

Reputation: 132

HTML

<div id="copy-text">Some Text</div>
<button onclick="copyToClipboard('#copy-text')" type="button" >Copy</button>

JS

function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
}

Upvotes: 0

David Gomez
David Gomez

Reputation: 2772

This is a recurrent question and it is a lot of "solutions" but mostly you will end up using flash if you want full platform support.

If that is not the case, you can use libraries like clipboard.js, and there's a lot more out there, just look for them.

Upvotes: 3

Related Questions