jQuery text method


jQuery text method : is used to read or write the text in the html.


Syntax of jQuery text() method

Get Text

$(selector).text();

Note : It does not accepts any argument.

jQuery text Method with syntax

jQuery text() Method – Example 1 – Get The Text

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#textExample" ).on( "click", function(event) {             
                 var txt = $("div").text();
                 alert(txt);
              });
               
          });
</script>

Try it »

Set Text

$(selector).text(text);

text: String,Number or Boolean to be set as content.

jQuery text Method with syntax

jQuery text() Method – Example 2 – Set The Text

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#textExample" ).on( "click", function(event) {             
                 $("div").text("I have been added using text() method...");
              });
               
          });
</script>

Try it »


Add Comment

📖 Read More


Advertisements