jQuery Tutorial Tutorials - jQuery .html() method

jQuery .html() method

The .html() method in jQuery is used to get/set the HTML contents of matched element.

jQuery .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.

example

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>

Try now

.html(htmlString)

Set the HTML contents of each matched elements.

example

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>

Try now

.html(function)

use function to set the HTML content

    $('p').html(function(index,oldHtml){
        return 'index:'+index+', old value:' +oldHtml;
    });

Try now

Date:2019-08-24 17:53:08 From:www.Lautturi.com author:Lautturi