MK Jain MK Jain - 25 days ago 13
HTML Question

Copy option in php using javascript not working

I am trying to copy content from copy button in PHP using javascript.

But it is not copying.

I tried:

<a class="btn btn-primary" href="user_images/<?php echo $row['userPic']; ?>"><span class="glyphicon glyphicon-eye-open"></span> View</span></a>
<p id="#i<?php echo $row['userID']; ?>" style="display: none;">user_images/
<?php echo $row['userPic']; ?>
</p>
<button onclick="copyToClipboard('#i<?php echo $row['userID']; ?>')" class="btn btn-primary"><span class="glyphicon glyphicon-eye-open"></span> Share</span>
</button>
<script>
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
</script>

Answer Source

Do not use # in id="". They are auto defined.

So, change <p id="#i<?php echo $row['userID']; ?>" to <p id="i<?php echo $row['userID']; ?>"

Hope this will work!