THE WORLD'S LARGEST WEB DEVELOPER SITE

jQuery html() Method

❮ jQuery HTML/CSS Methods

Example

Change the content of all <p> elements:

$("button").click(function(){
  $("p").html("Hello <b>world</b>!");
});
Try it Yourself »

Definition and Usage

The html() method sets or returns the content (innerHTML) of the selected elements.

When this method is used to return content, it returns the content of the FIRST matched element.

When this method is used to set content, it overwrites the content of ALL matched elements.

Tip: To set or return only the text content of the selected elements, use the text() method.


Syntax

Return content:

$(selector).html()

Set content:

$(selector).html(content)

Set content using a function:

$(selector).html(function(index,currentcontent))

Parameter Description
content Required. Specifies the new content for the selected elements (can contain HTML tags)
function(index,currentcontent) Optional. Specifies a function that returns the new content for the selected elements
  • index - Returns the index position of the element in the set
  • currentcontent - Returns the current HTML content of the selected element

Try it Yourself - Examples

Return element content
How to return the content of an element.

Set element content using a function
Using a function to set the content of all selected elements.


❮ jQuery HTML/CSS Methods