In this post, we are going to see jQuery text method example.
Live demo:
Jquery text method example on jsbin.com
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() :
1 2 3 4 5 |
$("selectors").text() Example : $("div").text(); |
Syntax for text(‘new text’) :
1 2 3 4 5 |
$("selectors").text('new text') Example: $("div").text('this text is being set by text method'); |
Let’s understand with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<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> <style> .blackBorder{ border:4px dotted black; </style> <script type="text/javascript"> $(document).ready(function(){ $("#textButton").click(function(){ alert($("div").text()) ; }); $("#setTextButton").click(function(){ $("div").text('this text is being set by text() method'); }); $("#reset").click(function(){ location.reload(); }); }); </script> <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> <br> <div class="blackBorder"> Welcome to JQuery. </div> |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.