---
title: "What are the use of .text() method in JQuery explain with example?"  
description: "What are the use of .text() method in JQuery explain with example?"  
author: "Ethan Karla"  
published: 2021-07-28  
canonical: https://answers.mindstick.com/qa/93802/what-are-the-use-of-text-method-in-jquery-explain-with-example  
category: "web application"  
tags: ["web application development", "web development"]  
reading_time: 2 minutes  

---

# What are the use of .text() method in JQuery explain with example?

What are the use of .text() [method in JQuery](https://www.mindstick.com/forum/156670/has-method-in-jquery) [explain](https://yourviews.mindstick.com/view/86025/content-created-for-humans-not-for-bots-explain-this-statement) with example?

## Answers

### Answer by Ravi Vishwakarma

The text [method](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business) is used for fetching the inner text of selected elements but in html we can get or set text with html tag, it is optional to add tag name in text.

```
Syntax
$(selector).text()
 Return the text of selected elements
$(selector).text(content)
 Set text in selected elements
$(selector).text(function(index,currentcontent))
 Set text in selected elements by using function
```

```
<!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 () {
                $('.div1').html('<p>Hello !!! i am added in div tag by using the html tag</p>');
                $('.div1').text('<b>Hello !!! i am added in div tag by using the text tag</b>');
            });
        });
    </script>
    <style>
        .div1 {
            margin:10px;
            padding:10px;
            width:200px;
            height:200px;
            background-color:gray;
            color:white;
        }
    </style>
</head>
<body>
    <button>Click me</button>
    <div class='div1'>
    </div>
</body>
</html>
```

![What are the use of .text() method in JQuery explain with example?](https://answers.mindstick.com/questionanswer/48dcd676-8b81-47c3-8d6b-c70a4506ab3f/images/f2a1c51b-7a5c-4cc9-849f-bb74e6c0530c.png)

\


---

Original Source: https://answers.mindstick.com/qa/93802/what-are-the-use-of-text-method-in-jquery-explain-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
