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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s