How can I create an text to clipboard function? I found on w3schools how to do it with an input but I don't know how to do it with just text (for example like Hypixel
Asked
Active
Viewed 278 times
-1
-
2Can you share the example you found? – NorSer Sep 28 '22 at 11:35
-
1"Copy text without Input and button" What action/event do you propose will activate the "copy to clipboard"? – bloodyKnuckles Sep 28 '22 at 11:37
-
1you at least have to use something a `span` a `paragraph` a `h1` a `div` – UmairFarooq Sep 28 '22 at 11:38
1 Answers
0
You don't need an library to do this.
Just use document.execCommand("copy")
$("#txt").on("click", function (event) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($("#txt").html()).select();
document.execCommand("copy");
$temp.remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="txt">Copy this text</div>

Webdeveloper_Jelle
- 2,868
- 4
- 29
- 55