Скопировать в буфер обмена js

У меня есть переменная login и password. Как мне по нажатию на кнопку (onlick), скопировать это: Ваш логин: {login} Ваш пароль: {password}


Ответы (3 шт):

Автор решения: Color kat

Было бы неплохо если бы вы сами нашли ответ - первая ссылка в гугле https://html5css.ru/howto/howto_js_copy_clipboard.php

/* Select the text field */
  copyText.select();

  /* Copy the text inside the text field */
  document.execCommand("copy");

→ Ссылка
Автор решения: Qwertiy

Может работать вот такое:

document.querySelector('button').addEventListener('click', e => {
  navigator.clipboard.writeText("Hi! I'm copied by button")
    .then(() => console.log("Done!"))
    .catch(err => console.error(err))
})
<button>Copy</button>

→ Ссылка
Автор решения: Qwertiy

А так даже во фрейме работает и без разрешений:

document.querySelector('button').addEventListener('click', e => {
  var inp = document.createElement('input')
  inp.value = "Hi! I'm copied by button"
  document.body.appendChild(inp)
  inp.select()
  
  if (document.execCommand('copy')) {
    console.log("Done!")
  } else {
    console.log("Failed...")
  }
  
  document.body.removeChild(inp)
})
<button>Copy</button>

→ Ссылка