I just want to add, if someone wants to copy two different inputs to clipboard. I also used the technique of putting it to a variable then put the text of the variable from the two inputs into a text area.
Note: the code below is from a user asking how to copy multiple user inputs into clipboard. I just fixed it to work correctly. So expect some old style like the use of var
instead of let
or const
. I also recommend to use addEventListener
for the button.
function doCopy() {_x000D_
_x000D_
try{_x000D_
var unique = document.querySelectorAll('.unique');_x000D_
var msg ="";_x000D_
_x000D_
unique.forEach(function (unique) {_x000D_
msg+=unique.value;_x000D_
});_x000D_
_x000D_
var temp =document.createElement("textarea");_x000D_
var tempMsg = document.createTextNode(msg);_x000D_
temp.appendChild(tempMsg);_x000D_
_x000D_
document.body.appendChild(temp);_x000D_
temp.select();_x000D_
document.execCommand("copy");_x000D_
document.body.removeChild(temp);_x000D_
console.log("Success!")_x000D_
_x000D_
_x000D_
}_x000D_
catch(err) {_x000D_
_x000D_
console.log("There was an error copying");_x000D_
}_x000D_
}
_x000D_
<input type="text" class="unique" size="9" value="SESA / D-ID:" readonly/>_x000D_
<input type="text" class="unique" size="18" value="">_x000D_
<button id="copybtn" onclick="doCopy()"> Copy to clipboard </button>
_x000D_