LearnFast.Ninja

jQuery write text to element (e.g. to div)

Create html element with id. Using jQuery select the element then use the text function write text to div. In the working example we create html div tag then at the head tag we use the jQuery ready function to add text to the div element when the page load so when the client will see the page the div element will have the text inside.

Step 1: Create html element with id. 

<div id="Div_With_Text">

</div>

 
Step 2: Using jQuery select the element then use the text function write text to div. 
$("#Div_With_Text").text('This is my text');
 
Working example: 

<html>

<head>

    <title>Page</title>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"></script>

    <script type="text/javascript">

        $(document).ready(function () {

            $("#Div_With_Text").text('This is my text');

        });

    </script>

</head>

<body>

    <div id="Div_With_Text">

    </div>

</body>

</html> 
 
Result:


Tags

jquery

jquery-tutorial-for-beginners text