Hi guys, i'm a grade 9 student and struggling with my small JS project. my teacher wants me to create a password generator. he wants me to create a Copy button that copies the random password that will display the word "copied" and display "field is empty" when blank. so far this is what i have </style> </head> <script language="javascript" type="text/javascript"> function randomString() { var chars = "00112233445566778899AaBbCcDdEdFfGgHhIiJjKkLLMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; var string_length = 8; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); } document.randform.randomfield.value = randomstring; } // Start copy button here function copy() { let textarea = document.getElementById("myInput"); textarea.select(); document.execCommand("copy"); } // END copy button </script> <form name="randform" align="center"> <input type="button" style="font-family:Consolas;color:#FFFFFF" class="button button1" value="Generate" onClick="randomString();"> <input type="text" id="myInput" name="randomfield" value="" style="color:blue; padding: 12px 16px; font-size:20px; font-family:Consolas; font-weight:bold; background:#FCF4A3; width: 10%;">  <input type="button" style="font-family:Consolas;color:#FFFFFF" class="button copybtn" value="COPY" onClick="copy('myInput');"> </form> HTML:
Here's a snippet you could use: function copyInput(id) { //Get input field reference var inputField = document.getElementById(id); //Select the text inputField.select(); //Execute the copy command document.execCommand("copy"); //BOOM alert(inputField.value + "copied to Clipboard"); Code (JavaScript): Then call the method like: onClick="copyInput('myInput');"> Code (JavaScript):