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

158
Vistas
Add "selectable" commas between multiple <span> items

I'm trying to add some commas between a list of items.

I know that you can do that by using :

.comma:not(:last-of-type)::after{
    content: ", ";
}
<span class="comma">A</span>
<span class="comma">B</span>
<span class="comma">C</span>

But with this technic the commas are not really written (you can't select them). I need to be able to select them because I then copy the text in the clipboard :)

Any ideas on how to do this (with css or js) ?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use document.querySelectorAll to select all but the last .comma span, and then add a comma to their innerText:

spans = document.querySelectorAll('.comma:not(:last-of-type)')

spans.forEach(span => span.innerText = span.innerText + ', ')
<span class="comma">A</span>
<span class="comma">B</span>
<span class="comma">C</span>

If you want to avoid placing commas after "empty" spans, you could check the length of the innerText before appending a comma:

spans = document.querySelectorAll('.comma:not(:last-of-type)')

spans.forEach(span => span.innerText = span.innerText + (span.innerText.length ? ', ' : ''))
<span class="comma">A</span>
<span class="comma"></span>
<span class="comma">C</span>

And if the spans might contain whitespace that you want to ignore, just trim the values first:

spans = document.querySelectorAll('.comma:not(:last-of-type)')

spans.forEach(span => span.innerText = span.innerText.trim() + (span.innerText.trim().length ? ', ' : ''))
<span class="comma">A </span>
<span class="comma">   </span>
<span class="comma"> C</span>

about 4 years ago · Santiago Trujillo 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