Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda