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

206
Views
How to change text content of multiple html tags using JavaScript?

For example:

<p> 1 </p>
<p> 2 </p>
<p> 3 </p>

As you can see that, there are 3 paragraph tag with the different number. So, using JavaScript read the content of the p tag and changed the text according to the content received from the p tag.

Suppose, first p tag has content 1 so changed it into "active". Second p tag has content 2 so changed it into "deactivated" with that third tag contain 3 so changed it into "others".

Where output seems like:

<p> Active </p>
<p> Deactivated </p>
<p> Others </p>

Even if we changed the p tag order to this:

<p> 2 </p>
<p> 3 </p>
<p> 1 </p>

the output must be:

<p> Deactivated </p>
<p> Others </p>
<p> Active </p>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use an array with the values you need, for each paragraph convert the original textValue to a number and retrieve the value from the array using that number.

Or use some kind of templating.

const values = [`Active`, `Deactivated`, `Others`];
document.querySelectorAll(`p`).forEach( (p) =>
  p.textContent = values[+(p.textContent.trim()) - 1]);
<p> 1 </p>
<p> 3 </p>
<p> 2 </p>
<p> 3 </p>
<p> 1 </p>
<p> 2 </p>

about 4 years ago · Juan Pablo Isaza Report

0

You can use switch and .innerText

const pEles = [...document.querySelectorAll("p")];

pEles.forEach(ele => {
  switch (ele.innerText) {
    case "1":
      ele.innerText = "Active"
      break;
    case "2":
      ele.innerText = "Deactivated"
      break;
    case "3":
      ele.innerText = "Others";
      break;
  }
})
<p>1</p>
<p>2</p>
<p>3</p>

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!