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

446
Views
Copie al valor de texto del elemento del Portapapeles usando jQuery

Editar: no intento copiar el área de texto o el valor de entrada, por lo que no es una pregunta duplicada como se sugiere.

¿Puedo usar copiar al portapapeles para el valor de texto de un elemento?

Como quiero copiar terra1

 <span id="terra-wallet-address">terra1</span>

Y jQuery:

 jQuery('#terra-wallet-address').focus(); jQuery('#terra-wallet-address').select(); document.execCommand('copy'); jQuery('.copied').text("Copied to clipboard").show().fadeOut(1200);

También probé .val() y .text() pero no funcionó.

Gracias.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Si no desea crear un área de texto temporal, puede seleccionar mediante programación el texto dentro del elemento (con Document.createRange y Range.selectNodeContents ) y luego llamar a execCommand .

Tenga en cuenta que deberá llamar a execCommand dentro de un controlador de eventos.

 var r = document.createRange(); var w = document.getElementById("terra-wallet-address"); r.selectNodeContents(w); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(r); btn.addEventListener('click', function() { document.execCommand('copy'); jQuery('.copied').text("Copied to clipboard").show().fadeOut(1200); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span id="terra-wallet-address">terra1</span> <button id="btn">copy</button>

about 4 years ago · Juan Pablo Isaza Report

0

Bueno, no puede focus() un elemento, pero puede usar otros métodos, como create range y luego ejecutar el comando copiar.

También creé una pequeña biblioteca que te permite copiar entradas, áreas de texto, divs y cualquier otro elemento: https://github.com/codingjlu/copyjs

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!