To change the inner HTML of an element using jQuery, the simplest way is to use the jQuery html() method:

$("#div").html("Changed Inner HTML Content");

Let’s say I have the following HTML:

Hello!

If I want to change the inner html inside the div from “Hi!” to “Bye!”, I can use the jQuery html() method to do this with the following Javascript code.

$("#div").html("Bye!");

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

jQuery("#div").html("Bye!");

We can also use the jQuery text() method to change the inner html of a HTML element only if the new content will only be text.

Change Inner HTML of Element 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 change the html of an element.

Let’s say we have the following code and we want to add html content to our paragraph.

Click here to update the inner HTML inside the paragraph.

We are going to change the HTML in this paragraph.

If we want to change the html content of the paragraph and add some bold text, 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(){
    $("#p-to-change").html("The html is now changed with some bold content.");
});

The final code and output for this example of how to change the inner html of an element using the jQuery html() method and Javascript is below:

Code Output:

Click here to update the inner HTML inside the paragraph.

We are going to change the HTML in this paragraph.

Full Code:

Click here to update the text inside the div.

We are going to change the HTML in this paragraph.

<script> $("#click-me").click(function(){ $("#p-to-change").html("The html is now changed with some bold content."); }); </script>

Add Elements to Inner HTML of Element with jQuery

If you want to add multiple elements to an element, we can do that as well with the jQuery html() method.

To add multiple elements or a more complex HTML structure to a HTML element, we just need to build the 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 update the inner HTML inside the div.
We are going to change the HTML in 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 the first paragraph added.

This is another paragraph to add.

And we add a third.

"); });

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

Code Output:

Click here to update the inner HTML inside the div.
We are going to change the HTML in this div.

Full Code:


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

This is the first paragraph added.

This is another paragraph to add.

And we add a third.

"); }); </script>

Hopefully this article has been useful for you to understand how to use jQuery to change the inner HTML of a element.

Categorized in:

jQuery,

Last Update: February 26, 2024