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

133
Vistas
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 Respuestas
Responde la pregunta

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