0

I have some HTML that lists lines of words, with each line attached to a class.

I also have a script function that does a global find and replace of the HTML for every occurrence of the word “Five”, replacing it with the word “Zero”.

However, the issue is that I only want to replace “Five” with “Zero” for every occurrence where class="second".


Question

How do I cycle through each line in the HTML making word replacements for only a specific class, for instance using the getElementsByClassName() method?


Code

Example of the word replacement in action - https://jsfiddle.net/yb0sLhqp/

<html>
<body>

<p class="first">One Two Three Four Five</p>
<p class="second">Three Four Five Six Seven</p>
<p class="third">Five Six Seven Eight Nine</p>
<p class="second">Three Four Five Six Seven</p>
<p class="first">One Two Three Four Five</p>
<p class="second">Three Four Five Six Seven</p>
<p class="third">Five Six Seven Eight Nine</p>
<p class="second">Three Four Five Six Seven</p>
<p class="first">One Two Three Four Five</p>

<button onclick="myFunction()">Replace</button>

<script>

function myFunction() {
document.body.innerHTML = document.body.innerHTML.replace(/Five/g, 'Zero');
}

</script>

</body>
</html>
1
  • so select the elements and replace the elements text as you loop over the collection. Commented Sep 18, 2019 at 16:16

2 Answers 2

2

So select all the elements with querySelectorAll, loop over, and replace the text.

function myFunction() {
  document.querySelectorAll(".second")  // select the elements
    .forEach(elem => // loop over
      elem.textContent = elem.textContent.replace(/Five/g, 'Zero') //replace the text
    )
}

myFunction()

/* without the fat arrow
function myFunction() {
  document.querySelectorAll(".second")  // select the elements
    .forEach(function(elem) { // loop over
      elem.textContent = elem.textContent.replace(/Five/g, 'Zero') //replace the text
    })
}
*/
<p class="first">One Two Three Four Five</p>
<p class="second">Three Four Five Six Seven</p>
<p class="third">Five Six Seven Eight Nine</p>
<p class="second">Three Four Five Six Seven</p>
<p class="first">One Two Three Four Five</p>
<p class="second">Three Four Five Six Seven</p>
<p class="third">Five Six Seven Eight Nine</p>
<p class="second">Three Four Five Six Seven</p>
<p class="first">One Two Three Four Five</p>

<button onclick="myFunction()">Replace</button>

Sign up to request clarification or add additional context in comments.

Comments

1

You can select element by className and then replace value

function myFunction() {
  let elements = document.getElementsByClassName("second");
  [...elements].forEach(element => {
    element.innerText = element.innerText.replace(/Five/g, 'Zero')
  })
}
<html>
<body>
  <p class="first">One Two Three Four Five</p>
  <p class="second">Three Four Five Six Seven</p>
  <p class="third">Five Six Seven Eight Nine</p>
  <p class="second">Three Four Five Six Seven</p>
  <p class="first">One Two Three Four Five</p>
  <p class="second">Three Four Five Six Seven</p>
  <p class="third">Five Six Seven Eight Nine</p>
  <p class="second">Three Four Five Six Seven</p>
  <p class="first">One Two Three Four Five</p>
  <button onclick="myFunction()">Replace</button>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.