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.
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>