.html() methodThe .html() method in jQuery is used to get/set the HTML contents of matched element.
.html() method Syntax.html() .html(htmlString) .html(function)
| Parameter | Type | Description |
|---|---|---|
htmlString |
htmlString | Html content to be set. |
function(index,oldHtml) |
Function | function return the HTML content to set.index is the position of the element in the set and oldHtml is the old HTML value. |
.html()Get the HTML contents of the first matched element.
get the html contens of first paragraph.
HTML
<p>Learn <strong>jQuery</strong></p> <p>Learn JavaScript</p>
jQuery
$("p").html();
Result
Learn <strong>jQuery</strong>
.html(htmlString)Set the HTML contents of each matched elements.
Change the content of the container
HTML
<div id="container">
<p>Learn JavaScript</p>
<p>Learn jQuery</p>
<p>Learn Python</p>
</div>
jQuery
$("#container").html('<h2>more tutorials on lautturi.com</h2>');
Result
<div id="container">
<h2>more tutorials on lautturi.com</h2>
</div>
.html(function)use function to set the HTML content
$('p').html(function(index,oldHtml){
return 'index:'+index+', old value:' +oldHtml;
});
