jQuery html() Method

jQuery html() method sets or returns html contents of the selected elements.

The html() method can be used to get the contents of any element. As well as you can set content and overwrites the content of all selected elements.

Syntax

This syntax return html content

$(selector).html();         // Return content

This syntax set HTML content

$(selector).html( htmlContent );         // Set content
Parameter Type Description
htmlContent String Required only if set new content.
HTML string content set on matched element.

Example: Return element content

return element content on selected elements.

$(document).ready(function(){
  $("button").click(function(){
    var content = $("p").html();
    $("div").text(content);
  });
});

Run it...   »

Example: Set element content

Set the content of all selected elements.

$(document).ready(function(){
  $("button").click(function(){
    $("div").html("<p>New text set on this element.</p>");
  });
});

Run it...   »