var copyUrl=document.getElementById("share-link");            
copyUrl.select(); // Select the object
document.execCommand("Copy"); // Execute the browser copy command

/* If it is an input field, select the text from the input field and call copy to copy the text to the clipboard. But select() only works with  and 

so my final solution was to add a

Copy the code