.html()

The .html() method gets or sets the contents of an HTML element.

Syntax

Getting HTML, the .html() method is called without a parameter:

$(selector).html();

Setting HTML, the .html() method is called with a parameter containing the new HTML:

$(selector).html(newHTML)

Example

The following jQuery code will read and reset the HTML in #p1 when #btn is clicked:

$("#btn").click(function() {
alert($("#p1").html());
$("#p1").html("<b>New Text.</b>");
});

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn JavaScript on Codecademy