I have a PHP script which will print out a list of items. I wanted to add a button on there which copies the one or more items in the list to the clipboard. I have found a basic Javascript function that seems to work nicely.
However if I get more than 2 lines, the copy function will only copy the first line. How can I do this correctly?
if (isset($_POST['create'])){
echo'<div class =" col-md-6 pull-right"><h3>Tokens you just generated below</h3><br>';
if((isset($_POST['create']) && ($insert_token && $stmt->rowCount())) >0){
foreach ($generatedtokens as $tokens){
$ListTokens = ($tokens['token'].' - '.$tokens['desc'].'<br>');
echo '<p id="tokens">'.nl2br($ListTokens).'</p>';
}
if (isset($_POST['create'])){
?>
<button onclick="copyToClipboard('#tokens')" class ="btn btn-primary">Copy Tokens</button>
<?php
echo '</div>';
}
}
}
?>
<script>
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
</script>