вы можете использовать input
для копирования текста в буфер обмена
function copy() {
/* Get the text field */
var copyText = document.getElementById("link");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
console.log("copied!");
}
<input id="link" onclick="copy()" value="https://www.spotify.com/us/">
Или, если вы хотите использовать p, вы можете использовать этот трюк
function copy() {
/* Get the text field */
var copyText = document.getElementById("link");
var copyP = document.getElementById("link_p");
copyText.value=copyP.innerHTML;
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
console.log("copied!");
}
<input type="text" id="link" value="" style="display:none">
<p id="link_p" onclick="copy()">text</p>