Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda