Because the navigator.clipboard.writeText
method returns a promise and your code does not wait for its result.
If you correct code as shown below then it should be fine:
function myFunction() {
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999);
navigator.clipboard
.writeText(copyText.value)
.then(() => {
alert("successfully copied");
})
.catch(() => {
alert("something went wrong");
});
}
For further information about Promise
and the navigator.clipboard.writeText
method, please visit the following links:
Promise on JavaScript.info
Interact with the clipboard on MDN