Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda