To replace the HTML inside a div using jQuery, the simplest way is to use the jQuery html() method:

$("#div").html("Replaced HTML Content");

Let’s say I have the following HTML:

This is some html in the div.

If I want to replace the html inside the div, I can use the jQuery html() method to do this with the following Javascript code.

$("#div").html("This is some new html with some bold text!");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div").html("This is some new html with some bold text!");

We can also use the jQuery text() method to replace the text inside a div only if the new content will only be text.

Replace HTML Inside Div On Click with jQuery

The jQuery html() method is very useful when it comes to manipulating web pages.

We can use the jQuery html() method to replace the html inside a div.

Let’s say we have the following code and we want to replace the html content inside our div.

Click here to update the HTML inside the div.

We are going to replace the HTML in this div.

If we want to replace the html content of the div and add some bold text to the paragraph, we can do this easily utilizing both the jQuery click() method and jQuery html() method.

Below is the Javascript code which will allow the user to be change the text and html content inside the div.

$("#click-me").click(function(){
    $("#div-to-change").html("

The html is now changed with some bold content.

"); });

The final code and output for this example of how to replace the html inside our div using the jQuery html() method and Javascript is below:

Code Output:

Click here to update the HTML inside the div.

We are going to replace the HTML in this div.

Full Code:

Click here to update the HTML inside the div.

We are going to replace the HTML in this div.

<script> $("#click-me").click(function(){ $("#div-to-change").html("

The html is now changed with some bold content.

"); }); </script>

Replacing Multiple Elements Inside a Div with jQuery

If you want to replace multiple elements inside a div, we can do that as well with the jQuery html() method.

If you want to change the HTML structure within a div, or add a complex HTML structure to a HTML element, we just need to build a string and then pass it to the jQuery html() method.

Let’s say we have the following code and we want to add multiple paragraphs to a div.

Click here to replace the HTML inside the div.
We are going to replace the HTML inside this div.

If we want to change the html content of the div and add multiple paragraphs, we can do this easily by building a string with all of the HTML elements we want, and then passing it to the jQuery html() method.

Below is the Javascript code which will allow us to add multiple paragraphs and change the inner html of our div.

$("#click-me").click(function(){
    $("#div-to-change").html("

This is one paragraph.

This is another paragraph.

And now a third paragraph.

"); });

The final code and output for this example of how to replace the html of a div and add multiple elements with a click using the jQuery html() method and Javascript is below:

Code Output:

Click here to replace the HTML inside the div.
We are going to replace the HTML inside this div.

Full Code:

Click here to replace the HTML inside the div.
We are going to replace the HTML inside this div.
<script> $("#click-me").click(function(){ $("#div-to-change").html("

This is one paragraph.

This is another paragraph.

And now a third paragraph.

"); }); </script>

Hopefully this article has been useful for you to understand how to use jQuery to replace the HTML inside a div.

Categorized in:

jQuery,

Last Update: February 26, 2024