jQuery html()

jQuery html() method can either be used to set content ( where it overwrites the content of all the matched elements) or can be used to return content ( where it returns the content of the first matched element).

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

Syntax:

When it is used to return content.

$(selector).html()

When it is used to set content.

$(selector).html(content)

Content: Content is a compulsory parameter, used to specify the new content for the selected elements.

When it is used to set content by calling function.

$(selector).html(function (index, currentcontent))

Function: It is an optional parameter that returns the new content on calling.

Index: It is passed as an argument inside the function to show the index position of the element.

Current content: This is another argument passed inside the function to show the current HTML content of the selected element.

Example:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").html("Hello! I am back.</b>");
});
});
</script>
</head>
<body>
<button>Click me</button>
<p>Bye Bye!</p>
</body>
</html>