jQuery text() method example

In this post,  we are going to see jQuery text method example.
text method is used to get text of all matched elements and text('new text') is used to set text for all matched elements. 
Syntax for text() :
Example :
Syntax for text('new text') :
$("selectors").text('new text')
 $("div").text('this text is being set by text method');

Let's understand with the help of example:
  <title>Jquery text method example </title>
  <script src="http://code.jquery.com/jquery-2.2.3.min.js"></script>
<h2>Jquery text method example</h2>
        border:4px dotted black;
<script type="text/javascript">
         alert($("div").text()) ;
         $("div").text('this text is being set by text() method');

  <button id="textButton">Using text</button>
  <button id="setTextButton">Using text('new text') </button>
<button id="reset">Reset</button>
<br />
<div class='blackBorder'>
 Hello world from java2blog!!!!
<div class='blackBorder' >
 Welcome to JQuery.
Live demo: Jquery text method example on jsbin.com

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012