Use the pseudo-class selector :containsto find any elements containing this text, then replace it.
$(":contains('old text')").each(function(){
$(this).text($(this).text().replace('old text', 'new text'));
});
If you know the list of elements you want to target, another solution would be to first find these elements and then filter them using: contains.
$('div, p, a, span').filter(":contains('old text')").each(function(){
$(this).text($(this).text().replace('old text', 'new text'));
});
source
share