jQuery text()

jQuery text() method can either be used to set text content ( where it overwrites the content of all matched elements) or can be used to return the text content ( where it returns the combined text content of all matched elements without the HTML markup).

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

Syntax:

When it is used to return text content.

$(selector).text()

When it is used to set text content.

$(selector).text(content)

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

When it is used to set text content by calling function.

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

Function: It is an optional parameter that returns the new text 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 text 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").text("Hello! I am back.</b>");
});
});
</script>
</head>
<body>
<button>Click me</button>
<p>Bye Bye!</p>
</body>
</html>