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 simple Example:

jQuery  text () 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 </title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#content').text("The jQuery text() method.");
        });
    </script>
</head>
<body>
    <div id="content">       
       
    </div>
</body>
</html>

 

OutPut:


set text of html element jquery


jQuery  text () 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 </title>
    <script src="Scripts/jquery-1.8.2.min.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>


OutPut:


set text of html element jquery


I hope this article will help to you.

 


0 comments


Leave a comment

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

you may also like