jQuery : string replace() and replaceWith() functions example

You can use jQuery string functions to replace strings using function replace() and replaceWith():
Please find the sample code below:

Add below scripts to your code to work with Datatables:

https://code.jquery.com/jquery-3.3.1.js

After adding the above links, use the following:


/======Start of the code======/
$(document).ready(function () {
$(".myclass").on("click", function () {
// use this way to replace complete string
$(".myclass p").replaceWith("Your new text is here");

//use this to replace selected string
$(".myclass p").text(function (index, text) {
return text.replace('Click me to erase old text', 'Your new text is here');
});
});
});
/======End of the code======/

That’s it you are done!

Leave a Reply