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

130
Visualizações
Inserting a tag around a specific character in a title

I need to replace 'No' with 'No' inside an h1 heading using vanilla javascript. So in a nutshell I would like to find the h1 tag by searching for it's class '.product__title', search the text inside the h1 tag and find every instance of 'No' and then wrap a sup tag around the 'o', like below

N<sup>o</sup>

And example of the heading could be 'Table No 04'

This is the code that I have tried, which finds and replaces the word - but I'm unable to insert the sub tag around the 'o', without the browser actually rendering the sub tag as text and not html.

var html = document.querySelector('.product__title');
  var walker = document.createTreeWalker(html, NodeFilter.SHOW_TEXT);
  var node;
  while (node = walker.nextNode()) {
    node.nodeValue = node.nodeValue.replace(/No/, 'N<sup>o</sup>')
  }

Any help would be highly appreciated. The above code is from a tutorial I found searching around, so it's not my original code.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Why so complicated?

[...document.querySelectorAll("h1.product__title")]
  .forEach(h1 => h1.innerHTML = h1.textContent.replace("No ","N<sup>o</sup> "))
<h1 class="product__title">Table No 04</h1>
<h1 class="product__title">Table No 05</h1>

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