Replacing text with jQuery
Replacing text via jQuery that matches certain word(s) -- and replaces with new word.
by Jon Fuller
HTML
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<div class="wrapper">
<h2>Old Text 1</h2>
<p class="intro">Hello text, this is some old text that was already in the document.</p>
<div class="example">
<a href="#0">Old Text Link</a>
</div>
</div>
JavaScript
// replaces all text within element
$(".wrapper .intro:contains('some old text')").html("New1");
// replaces only the text mentioned... and only the first instance within selector
jQuery(function($){
$(".wrapper a").text(function () {
return $(this).text().replace("Old", "New2");
});
});