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

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

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