jQuery Document Operation - html() Method

Example

Set the content of all p elements:

$(".btn1").click(function(){
  $("p").html("Hello <b>world</b>!");
});

Try It Yourself

Definition and Usage

The html() method returns or sets the content (inner HTML) of the selected element.

If this method does not set any parameters, it will return the current content of the selected element.

Return element content

When using this method to return a value, it will return the content of the first matching element.

Syntax

$(selector).html()

Try It Yourself

Set element content

When using this method to set a value, it will override the content of all matching elements.

Syntax

$(selector).html(content)

Try It Yourself

Parameters Description
content - Optional. Specifies the new content of the selected elements. This parameter can include HTML tags.

Use a function to set element content

Use a function to set the content of all matching elements.

Syntax

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

Try It Yourself

Parameters Description
function(index, oldcontent)

Specifies a function that returns the new content of the selected elements.

  • index - Optional. Receives the index position of the selector.
  • oldcontent - Optional. Receives the current content of the selector.