1

I'm creating a search site for video content. In the results, I'd like to have a button that will copy to the clipboard the Video name and Video URL that I have in the database. I've found a JS function that will copy set text("Copied text" in the code below). I need the function to copy a variable(from an array), which will change, depending what someone is searching for.

<button onclick="myFunction()">Copy Video Name & URL</button>

function myFunction() {
    var copyText = "Copied text";
    let input = document.createElement('input');
    input.setAttribute('type', 'text');
    input.value = copyText;
    document.body.appendChild(input);
    input.select();
    document.execCommand("copy");
    document.body.removeChild(input)
} 
  • Does this answer your question? [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – tadman Oct 30 '20 at 21:29
  • Where does this array come from? You have used tag `php`, could you please highlight how you are expecting the JS to know which values to pick up? e.g. is this a 1 option per page, or perhaps different videos will be listed with their own buttons? etc. If you explain your desired outcome we can try help you out – Simon Oct 30 '20 at 21:34
  • To be more clear, the search will often bring up more than 1 answer. i.e. if searching for "jazz", there could be 20 videos that come up in a table. i.e echo " ".$row["VideoName"].", etc. I can create a concatenated variable with the 2 var - Video Name & Video URL. Just trying to figure where it goes in the function to make it all work. Thx – Brian Delaney Oct 31 '20 at 02:01

0 Answers0