Javatpoint Logo

91-9990449935

 0120-4256464

jQuery html()

jQuery html() method is used to change the entire content of the selected elements. It replaces the selected element content with new contents.

Note: It is a very useful function but works in a limited area because of its API documentation. The API documentation of the jQuery html function consists of three method signatures.

The first method signature has no argument, so it just returns the HTML within that element. The remaining two signatures take a single argument: i.e. a string or a function that returns a string.

Syntax:

It is used to return content.

It is used to set content.

It is used to set content by calling function.

The jQuery html() method is used either for set the content or return the content of the selected elements.

  • To set content: When you use this method to set content, it overwrites the content of the all matched elements.
  • To return content: When you use this method to return content, it returns the content of the first matched element.

The text() method is used to set or return only the text content of the selected elements.

Parameters of jQuery html() method

Parameter Description
Content It is an essential parameter. It is used to specify the new content for the selected elements. It can also contain HTML tags.
Function (index, currentcontent) It is an optional parameter. It specifies a function that returns the new content for the selected elements.
  • Index: It shows the index position of the element in the set.
  • Currentcontent: It shows the current HTML content of the selected element.

Example of jQuery html() method

Let's take an example to demonstrate jQuery html() method. It is changing the content of all p elements.

Test it Now

Output:

This is a paragraph.

This is another paragraph.


jQuery html() example 2

Let's see another example of jQuery html() method that returns HTML content. It returns the content of first paragraph only.

Test it Now

Output:

This is first paragraph.

This is another paragraph.


jQuery html() example 3

Let's see another example of jQuery html() method that converts HTML to text.

Test it Now

Output:

Click here to change the html to text

Next TopicjQuery text()