How can I select textcontent before it is copied to the clipboard? Here is an idea of selecting with one click: https://stackoverflow.com/a/1173319. But this is an other code with id selector. I only want to select textcontent with a class before copy it. Below is the code that is used to make a copy (onclick element with class as classname):
spans = document.querySelectorAll(".class");
for (const span of spans) {
span.onclick = function() {
document.execCommand("copy");
}
span.addEventListener("copy", function(event) {
event.preventDefault();
if (event.clipboardData) {
event.clipboardData.setData("text/plain", span.textContent);
}
});
}
<p class="class">
p
</p>
<span class="class">
span
</span>