There are two methods jQuery html() and jQuery replaceWith() by which jquery replace HTML content of a page. However, there is the major difference between these two methods you can notice by looking at the examples given below.

Let’s start learning how jQuery replace HTML content of a page.

jQuery Replace HTML Content Using html()

The function replaces the inner content of the selected HTML element. However, you can add HTML tags inside the html() to replace with the selected HTML element.

To learn more about the syntax of HTML methods you can visit jQuery html() page.

Output


This is a paragraph with HTML content.

The above example replaces the content of the selected HTML paragraph tag using the id. The replaced content contains the HTML span tag and HTML strong tag.

Click the above given button to replace the content using the html().

jQuery Replace HTML Content Using replaceWith()

The jQuery html() replaces the content of the selected HTML element but jQuery replaceWith() replaces selected HTML element as well as its content. See the below example to learn how to do this.

Output


This is a paragraph with HTML content.

The above example replaces the HTML element paragraph with the specified HTML content in the script. Click the above-given button to change the HTML element and its content.

Looking for more examples? Click below-given examples to see each one.

List of Examples

Output


This is my paragraph.

Output


Hello World

Welcome to Tutorialdeep web tutorial.

Learn new things each day.

Leave a Reply

Your email address will not be published. Required fields are marked *