Home >>jQuery Tutorial >jQuery html() Method

jQuery html() Method

jQuery html() Method

jQuery html() method in jQuery is used to returns the content of the selected elements.

It overwrites the content of ALL matched elements, when this method is used to set content.

Syntax:
Return content: $(selector).html()
Set content: $(selector).html(content)
Set content using a function: $(selector).html(function(index,currentcontent))

Parameter Values

Parameter Description
content It is a Required parameter and is used to Specifies the new content of the selected elements
function(index,currentcontent) It is Optional parameter and is used to Specifies a function that returns the new content of the selected elements
  • index – used to Returns the index position of the element
  • currentcontent – used to Returns the current HTML content of the selected element
Here is an Example of jQuery html() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$(".txt1").html("Php <b>Tpoint!</b>");
});
});
</script>
</head>
<body>
<button class="btn1">click me to Change content of all p elements</button>
<p class="txt1">This is a first paragraph.</p>
<p class="txt1">This is second paragraph.</p>
</body>
</html>

Output:

This is a first paragraph.

This is second paragraph.