Home >>jQuery Tutorial >jQuery text() Method

jQuery text() Method

jQuery text() Method

jQuery text() method in jQuery is used to returns the text content of the selected elements and returns the text content and overwrites the content of ALL matched elements.

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

Parameter Values

Parameter Description
content It is a required parameter and is used to Specifies the new text content for the selected elements
function(index,currentcontent) It is optional parameter and is used to specifies a function that returns the new text content for the selected elements
  • index – used to returns the index position of the element
  • currentcontent – used to returns current content of selected elements
Here is an Example of jQuery text() 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").text("Hello Phptpoint!");
});
});
</script>
</head>
<body>
<button class="btn1"> Click me to set text content</button>
<p class="txt1">This is First paragraph.</p>
<p class="txt1">This is Second paragraph.</p>
</body>
</html>

Output:

This is First paragraph.

This is Second paragraph.


No Sidebar ads