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

144
Views
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 answers
Answer question

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 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!