I've been trying to copy text to the clipboard when clicking on an image but after many failed attempts, I'm stuck. Here's where I am now:
function myFunction() {
var copyText = document.getElementById("myInput");
copyText.select();
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
<input type="text" value="text which you want copy" id="myInput" hidden>
<img src="nameofimage.jpg" onclick="myFunction()"></img>
Does anyone have a solution?
vanowm's solution below works perfectly.