1

I am using hadlerbars as the view engine for express and i want the text in the variable code to be copied to the clipboard i tried many solutions . This is my current code

function copyLink() {
    var copytext = document.getElementById('alcslink').innerHTML
    let code = copytext.split(":- ").pop() /*formatted */
    code.select();
      code.setSelectionRange(0, 99999); /* For mobile devices */
    
       /* Copy the text inside the text field */
      navigator.clipboard.writeText(code.value);
    
}

when i run this it gives me this error in the web console

 TypeError: code.select is not a function
Naveen Ravi
  • 113
  • 1
  • 8

1 Answers1

0

This is how I use it:

Note, it does not work in the snippet engine. You will need to add it to your code. Sorry.

const writeToClipboard = async (txt) => {
  const result = await navigator.permissions.query({ name: "clipboard-write" });
  if (result.state == "granted" || result.state == "prompt") {
    await navigator.clipboard.writeText(txt);
    console.log('Copied to clipboard');
  }
};

document.querySelector('button').addEventListener('click', async () => {
 const el = document.querySelector('div');
 await writeToClipboard(el.innerHTML);
});
<div>copy me</div>
<button>Click</button>
Bibberty
  • 4,670
  • 2
  • 8
  • 23