Introduction:

In this article we will explain how to set or get text in a html elements using jQuery or What is text() method in jQuery or why use text () method in jQuery with example.

Description:

The jQuery text () method is used to set or get the text content of the selected html elements. The text() method cannot be used on form input controls.  

Let’s see, Example of jQuery set() method: (set text content)

In this example we set the text content of div elements.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Example of jQuery text method</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#content').text("The jQuery text() method.");
        });
    </script>
</head>
<body>
    <div id="content">
 
    </div>
</body>
</html>  

Let’s see, Example of jQuery get() method: (get text content)

In this example we get the text content from div element and set the text of p element.

!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Example of jQuery text method</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var content = $('#content').text();
            $('p').text(content);
        });
    </script>
</head>
<body>
    <div id="content">
        The jQuery text() method .
    </div>
    <p></p>
</body>
</html>


 Let's check live example

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like