-1

I have a landing page where I am trying to use JS to find and replace all instances of a string across a certain tag.

Currently it looks like this:

<span class="h4">Some Text</span>

And I want it to look like this (replacing "Text" with "New Text")

<span class="h4">Some New Text</span>

Note that there is more than one instance of this on the page, I am not just trying to update this once. I have tried using the .replace method but can't seem to get this to work - any help would be appreciated!

JackNesbitt
  • 125
  • 1
  • 7

1 Answers1

-1

You can try this-

const h4 = document.querySelectorAll('.h4');
h4.forEach(el => {
  el.innerHTML = el.innerHTML.replace(/text/gi, 'New Text');
});
<div class="h4">Some Text</div>
<div class="h4">Some Value</div>
<div class="h4">Some Text</div>
<div class="h4">Some Text Text</div>
<div class="h4">Some Text</div>
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30