Using JavaScript to Find & Replace by Text / Copy in an Element

First you need to locate the parent element that contains the phrase you are looking to replace. If using a template, such as WordPress you can do this by finding the element by class, Id or even tag. In this example we use Class.

var ParentClass = document.getElementsByClassName('woocommerce-product-details__short-description');

Next you need search within that class for the phrase your wanting to change.

for (var i = 0; i < ParentClass.length; i++) {
ParentClass[i].innerHTML = ParentClass[i].innerHTML.replace("Old Text","NEW TEXT")
} 

Leave a Reply