jQuery | text() Method
This method is used to set or return the text content of the element. While setting the content, it overwrites the content of all the matched elements. The returned content of text method() is used to return the text content of all matched elements.
Syntax:
- Return text syntax:
$(selector).text()
- Set text syntax:
$(selector).text(content)
- Set text using a function:
$(selector).text(function(index, currentcontent))
Property Values:
- Content Required: It is used to set the new text content for the element.
- Function(index, currentcontent): It is used to specify a function that will return the new text content for the selected elements.
- index: It returns the index position of the element.
- currentcontent: It returns the current content of element.
Example-1: Set text syntax.
<!DOCTYPE html> <html> <body> <h1> <center> Geeks <button onclick="function()">Click me </button> </center> </h1> <script src= </script> <script> $(document).ready(function() { $("button").click(function() { $("p").text("Jquery_text_method"); }); }); </script> <p>GeeksforGeeks</p> <p>Jquery</p> </body> </html> |
Output:
Before clicking on the button:

After clicking on the button:

Example-2: Return text syntax.
<!DOCTYPE html> <html> <body> <h1> <center> Geeks <button onclick="function()">Click me </button> </center> </h1> <script src= </script> <script> $(document).ready(function() { $("button").click(function() { alert($("p").text()); }); }); </script> <p>GeeksforGeeks</p> <p>Jquery</p> </body> </html> |
Output:
Before clicking on the button:

After clicking on the button:

Example-3: Set text using a function.
<!DOCTYPE html> <html> <body> <h1> <center> Geeks <button onclick="function()">Click me </button> </center> </h1> <script </script> <script> $(document).ready(function() { $("button").click(function() { $("p").text(function(n) { return "Index No. of Element: " + n; }); }); }); </script> <p>GeeksforGeeks</p> <p>Jquery_textmethod()</p> </body> </html> |
Output:
Before clicking on the button:

After clicking on the button:

Recommended Posts:
- jQuery | :text Selector
- How to Change the Text of a Button using jQuery?
- JQuery | Get the text of a span element
- JQuery | Set the value of an input text field
- How to make a blinking text using jQuery ?
- How to get text of specific option tag using jQuery?
- JQuery | Change the text of a span element
- How to get selected text from a drop-down list using jQuery?
- JQuery | Set focus on a form input text field on page load
- How to select all text in HTML text input when clicked using JavaScript?
- HTML | DOM Input Text select() Method
- jQuery | get() Method
- jQuery | is() Method
- jQuery | before() Method
- jQuery | die() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



