Use querySelector to replace specific text

Hello Guys,

Can you help point me to changing specific text to something else using javascript?
I have this:

document.querySelector("#shopping-cart-table > tbody > tr.first.odd > td.a-left.product-name-td > div > div:nth-child(1) > b")
.innerHTML = "Change to this specific text";

That changes the text, but not specific text. It should replace specific text in that location. Any help is much appreciated. Thanks :slight_smile:

You can do that more easily by breaking the line up in to different tasks.
Modifying your current code, that gives:

var selector = "#shopping-cart-table > tbody > tr.first.odd > td.a-left.product-name-td > div > div:nth-child(1) > b";
var el = document.querySelector(selector);
el.innerHTML; = "Change to this specific text";

With that structure in place, it’s much easier to achieve what you are after, for example, by changing the last line.

el.innerHTML = el.innerHTML.replace("content to find", "content to replace it with");
1 Like

That works great, thank you Paul :clap:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.