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
Use XPath to select all values on a page and replace content

I want to replace every instance of the word "Specialization" with "Profession." This is the XPath code I have so far:

<script type="text/javascript">

var xpath = "//div[text()='Specialization']"; 
var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
matchingElement.style.color = "red";

</script>

My thanks to @Mateen for their help with my original question for writing the above line for me in the first place.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure why you need an XPath solution, but if you want to stick with the standard DOM API, here's a simple solution.

document.querySelectorAll("div").forEach(function(d){
  if(d.textContent.includes("Foo")){
    d.textContent = d.textContent.replace("Foo","FooBar");
    d.classList.add("match");
  }
});
.match { color:#f00; }
<div>Foo is cool</div>
<div>Bar is not</div>
<div>Foo is cool</div>
<div>Bar is not</div>
<div>Foo is cool</div>
<div>Bar is not</div>
<div>Foo is cool</div>
<div>Bar is not</div>
<div>Foo is cool</div>
<div>Bar is not</div>
<div>Foo is cool</div>
<div>Bar is not</div>

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!