0

I set a copy button in my code. The function is when user click copy button a random item from list will be copy. its works fine. But my problem is I want different item each click... ( not same as any previous item)

My random pic item code is

var myArray = ['name1', 'name2', 'name3','name4', 'name5', 'name6', 'name7', 'name8', 'name9', 'name10'];

function copyFunction() {
  var rendtxt = myArray[~~(Math.random() * myArray.length)];
  const textArea = document.createElement('textarea');
  textArea.textContent = rendtxt;
  document.body.append(textArea);
  textArea.select();
  document.execCommand("copy");
  button.innerText = "Text copied";
  textArea.remove();
}
document.getElementById('button').addEventListener('click', copyFunction);

You can check output here

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    Shuffle the array randomly at the beginning. Then pick the next one each time. – Barmar Jun 21 '22 at 08:53
  • 1
    Does this answer your question? [How to efficiently randomly select array item without repeats?](https://stackoverflow.com/questions/17891173/how-to-efficiently-randomly-select-array-item-without-repeats) – pilchard Jun 21 '22 at 08:54
  • also [Random item from the array without repeating elements in Javascript](https://stackoverflow.com/questions/61556115/random-item-from-the-array-without-repeating-elements-in-javascript) – pilchard Jun 21 '22 at 08:55

0 Answers0