I tried alert(id) to see if the function is getting the id properly.
When I reload the page the function automatically copies the last row and alerts the id of the last row.
I also tried to take the script; inside php, still not working
<?php
$sql= "SELECT * FROM post ORDER BY id DESC";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$cap = $row['cap'];
echo "<div class='tooltip'>";
echo "<button onclick='myFunction()' onmouseout='outFunc()'>
<span class='tooltiptext' id='myTooltip'>Copy to clipboard</span>
Copy text
</button>
</div>
<input type='text' value='$cap' id='$id'>";
?>
<script>
function myFunction() {
var copyText = document.getElementById("<?php echo $id; ?>");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copied: ";
}
function outFunc() {
var tooltip = document.getElementById("myTooltip");
tooltip.innerHTML = "Copy to clipboard";
}
</script>