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

265
Views
I need some help using JavaScript to wrap <span> tags around existing content

Hey appreciate any help I can get with this as I'm still learning JavaScript.

I have some HTML that I'm trying to insert specific span tags into. I've found answers that allow you to just replace inner HTML, however the content I'm trying to wrap around is slightly different on each page I'm trying to achieve this for - but the HTML structure is the same.

Here is the current HTML, vs what I would like it to be:

<div class="list-view__count">
<p>
Showing 1 - 18 of 46 results
</p>
</div>

And what I'd like to add in:

<div class="list-view__count" content="nosnippet">
<p>
<span data-nosnippet>Showing 1 - 18 of 46 results</span>
</p>
</div>

I have tried various codes and successfully added the content="nosnippet" section,

document.querySelector(".list-view__count ").setAttribute("content", "nosnippet")

but the tag I can't seem to figure out how it is done yet.

Any help will be appreciated.

Cheers

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

0

try something like

    var div = document.getElementById('list-view__count');
    var x = document.creatElement('span');
    var text = document.createTextNode('text here');
   x.appendChild(text)
    div.appendChild(x)
about 4 years ago · Juan Pablo Isaza Report

0

  • You can get the text inside the <p> with textContent, save it in a variable, then create a span tag and set the text to the <span> with the same method.
  • I don't think you can set a data... attribute without a value like that, but functionally it should be the same.
  • Wrapped it in another div so it's easier to visualize the output.

const div = document.querySelector(".list-view__count");
const p = document.querySelector(".list-view__count>p");
const pText = p.textContent;
const span = document.createElement("span");

div.setAttribute("content", "nosnippet")
p.textContent = '';
span.textContent = pText;
span.setAttribute("data-nosnippet", '')
p.appendChild(span);

console.log(document.querySelector(".wrap").outerHTML)
<div class="wrap">

    <div class="list-view__count">
    <p>
    Showing 1 - 18 of 46 results
    </p>
    </div>

  </div>

about 4 years ago · Juan Pablo Isaza Report

0

You can select the p element that is the immediate child of your div, pick up its innerHTML (I did this just in case it had some HTML mark up in it so that is preserved), clear the innerHTML of the p element then append a span element which has the data- attribute set and the innerHTML copied from the original p.

const p = document.querySelector('.list-view__count > p');
const pInner = p.innerHTML;
p.innerHTML = '';
const span = document.createElement('span');
span.innerHTML = pInner;
span.setAttribute('data-nosnippet', '');
p.append(span);
<div class="list-view__count">
  <p>
    Showing 1 - 18 of 46 results
  </p>
</div>

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!