jQuery html() Method

The jQuery html() method can be used to return or set the HTML content for the selected element. You can add or get HTML tags and their content using this method.

jQuery html() Method

Syntax

The syntax of this method is given below:-

Syntax
$(selector).html()
Syntax
$(selector).html(content)
Syntax
$(selector).html(function(index, current_content))

Description of the Parameters

The description of these parameters is given below.

Parameter Name Description
selector It is the element to select and set or return the HTML content. You can use tag name, class name, or id of the elements to select. It is a required field.
content Specify the HTML content to set or return for the selected elements. It is a required field. The element can contain HTML tags to add to the selected element.
index Returns the index position of the element.
current_content It returns the current HTML content of the selected element.

jQuery html() Method to HTML Content

If you want to return or get the HTML content of the selected element, you have to use the method as given below. It requires no parameter to specify and return the HTML content.

Example

Test it Live

Output

This is my paragraph. Welcome to TutorialDeep!.


When you click the button given above, it returns the HTML content and displays it on the alert box. The alert displays the content including the HTML tags and its content.

Set HTML Content Using jQuery html() Method

If you want to set the HTML content of the selected element, you have to use the method as given below. To set the HTML content, you have to specify the HTML content including tags.

Example

Test it Live

Output

This is my paragraph.


You have to click the button given above to set the specified HTML content. It replaces the content of the selected element with the specified HTML content including tags.

Use Function to Set the HTML Content

The method can also be used with the function to set the HTML content for the specified element. You have to use the example as given below to set the HTML content.

Example

Test it Live

Output

This is my paragraph.


Click the button given above to set the HTML content using the function. The method replaces the selected content with the specified HTML content inside the function.

You may also like to read

I hope you like this tutorial on jQuery html() method. If you have any queries regarding the tutorial, please comment below.

References