• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

162
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 3 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 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error