I want that when i press the button it will copy the "hello" but i get this error: copyText.select is not a function
how to fix it?
<html>
<head>
</head>
<body>
<span id="aaa">hello</span>
<button onclick="copy('#aaa')">copy</button>
<script>
function copy(text)
{
var copyText = text;
copyText.select();
document.execCommand("copy");
console.log(document.getElementById('text'));
}
</script>
</body>
You cannot select text of a span using select function. You have to create a temporary input with the value of the text of the span. select the value in the input using select() and copy the text and then delete the input
function copy(text) { var copyText = document.getElementById(text).textContent; document.querySelector('#aux').innerHTML+=('<input id="a" value='+copyText+'>') document.getElementById("a").select(); document.execCommand("copy"); document.querySelector('#aux').innerHTML=""; //console.log(document.getElementById('text')); }
<html> <head> </head> <body> <span id="aaa">hello</span> <button onclick="copy('aaa')">copy</button> <span id="aux"></span> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.