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

135
Views
Add copy to clipboard feature to regex variable output

I'm trying to write some code to simplify copying just the number from a bit of code that generates a mix of letters and a decimal value. I've so far been successful using a regex (my first time using one so if it's not perfect that's why), to simplify the data down to just the decimal number.

I now want the decimal number to be copied to the clipboard, to make things easier for the user. I've tried using the deprecated document.execCommand('copy') and navigator.clipboard.writeText(myvariable). This is how my function currently looks like, with a commented out graveyard of attempts.

In practice files, I can run implement copying to clipboard on button clicks for text that doesn't change, but it doesn't seem to like getting the value from the variable. As I've said in a previous post, Javascript is still new to me, so it's likely just a simple beginner error.

const copy = document.getElementById("copyPair")
    copy.addEventListener("click", function(){

console.log("copy clicked");
        const amount = document.getElementById("pair-price").innerText;
        let decimal = Number(amount.replace(/[^0-9\.]+/g,""));
        alert(decimal);
        console.log(decimal);

/*    decimal.select();
      decimal.setSelectionRange(0, 99999); /!* For mobile devices *!/
      navigator.clipboard.writeText(decimal.value);

      let text = decimal;
      navigator.clipboard.writeText(decimal.value);
      
      document.execCommand('copy');

Any and all help getting copying the variable implemented would be very much appreciated!

about 4 years ago · Juan Pablo Isaza
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!