konweb
10/3/2019 - 5:25 AM

JS Clipborad Copy

JS Clipborad Copy

document.addEventListener("DOMContentLoaded", (ev) => {
  const copyEl = document.querySelector('#copy')

  copyEl.addEventListener("click", (ev) => {
    ev.preventDefault()

    const input = document.createElement('input')
    input.readOnly = true
    input.value = location.href

    document.body.appendChild(input)

    input.select()
    input.focus()
    input.setSelectionRange(0, input.value.length)

    const command = document.execCommand('copy')
    
    document.body.removeChild(input)
  })
});