Reputation: 69
I am trying to use the following code that copies text to your clipboard, but when I click on the text, it does not copy anything, and I have no JS errors in the console. (I'm sure it's something to do with the use of "element" within the jQuery, but I'm not sure how to fix it.)
I do not want to assign an ID to the Phone number, only enter the number manually within the "onclick" event.
Html
<p style="text-decoration:underline;" onclick="copyToClipboard('1-888-888-8888')"> My Phone Number</p>
jQuery
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
Upvotes: 0
Views: 235
Reputation: 524
I think the problem is in this line:
$temp.val($(element).text()).select();
Change this into this:
$temp.val(element).select();
And you will see the result
Upvotes: 1