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

119
Vistas
Copy more than one div to clipboard and adding characters

I am using the following script to copy a div to clipboard. But I am trying to copy multiple divs (DivA + DivB) with the same button, while adding some quotes and brackets around each div. I saw some answers (like this one, and this), but I can't seem to be able to implement them to the current script.

So the output should be like:

"A certain quote" (Author Name).

This is the current script to copy one div.

function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="divA">
<p>A certain quote</p>
</div>

<div id="divB">
<p>Author Name</p>
</div>

<button onclick="copyToClipboard('#divA')">Copy</button>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue is because you're only reading the text from '#divA', as that's the selector passed to the copyToClipboard() function.

To do what you require amend the logic to create a string in the format you require containing the text of both #divA and #divB:

let $divA = $('#divA');
let $divB = $('#divB');

$('button').on('click', e => {
  copyToClipboard(`"${$divA.text().trim()}" (${$divB.text().trim()}).`);
});

function copyToClipboard(text) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val(text).select();
  document.execCommand("copy");
  $temp.remove();
}
textarea {
  width: 300px;
  height: 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="divA">
  <p>A certain quote</p>
</div>
<div id="divB">
  <p>Author Name</p>
</div>
<button type="button">Copy</button><br /><br />

Paste here to test:<br />
<textarea></textarea>

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