Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
How to copy a text, using onclick html button?

I create my own portfolio website, on the navigation bar, there is my email. I want anybody cliking on this email will copy it to the clipboard. That's what i tried but it's not working !

<button onclick="copy()" id="copy">Copy</button>

function copy() {
var copyText = document.querySelector("#copy"); 
copyText.select(); document.execCommand("copy");}
document.querySelector("#copy").addEventListener("click", copy);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Note that there is an ability to "request permission" and test for access to the clipboard via the permissions API in Chrome 66.

var text = "text to be copied"
navigator.clipboard.writeText(text).then(_ => {
  console.log('copied successfully!')
})
about 4 years ago · Juan Pablo Isaza Report

0

Buttons do not have a select method, text boxes do.
You can use a text input to hold the email address to copy to the clipboard.

function copy() {
  var copyText = document.querySelector("#email"); 
  copyText.select(); document.execCommand("copy");
}
document.querySelector("#copy").addEventListener("click", copy);
input {margin:-100%}
<button onclick="copy()" id="copy">Copy</button>
<input id=email value="email@so.com"> 

Buttons do not have a select method, textboxes have it.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!