Javascript Ajax jQuery Html PHP Example Quiz New MORE
Need a Website Or Web Application Or Any Help.Contact Us: +91 9437911966 (Whatsapp) Note: Paid Service

jQuery text() method With Example

To get or set text content for a selected element in jQuery html() method is used. It is an inbuilt method of jQuery.

Syntax:

$(selector).before(content,function(index))

Example:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#set_button").click(function(){
    $("div").text("I am Set in this div element.");
  });
  $("#get_button").click(function(){
    var data=$("div").text();
    alert(data);
  });
});
</script>
</head>
<body>
 
<button id="set_button">Set the content</button>
<br><br>
<div id="div1" style="width:200px;height:80px;background-color:palegreen;">html() method jQuery</div><br>
 
<button id="get_button">Get the content</button>
<p>By click on the get the content button you can get the data of the div element</p>
</body>
</html>
Run it Yourself »

Difference between text() and html() method:

To get or set only text content we used text() method
To get text with html markup we used html() method.