Search and Replace All Instances of text that has the same name

jquery

Solution

Use the `:contains` pseudo-class selector to find any elements containing that text, then replace from there.

$(":contains('old text')").each(function(){
    $(this).text($(this).text().replace('old text', 'new text'));
});

If you know a list of elements that you'd like to target, another solution would be to first find those elements, then filter over them using :contains.

$('div, p, a, span').filter(":contains('old text')").each(function(){
    $(this).text($(this).text().replace('old text', 'new text'));
});

Problem

I am trying to do a search and replace of text that has the same name inside the body but it is partial working, some of the text is being replace, but then some of the old text still show up, not quite sure what is going on. ``` $(document).ready(function() { $('*').each(function(){ if($(this).children().length == 0) $(this).text($(this).text().replace("old text", "replace with new text")); }); }); ``` much help is appreciated thanks

Original source

Related problems