The .html() and .text() methods in jQuery are used to get or set the contents of an HTML element. The main difference between the two methods is that .html() returns or sets the entire HTML content of an element, including any HTML tags, while .text() only returns or sets the text content of an element, without any HTML tags.
Here are some examples of how to use the .html() and .text() methods:
Code snippet
// Get the HTML content of the first `div` element
var html = $("#myDiv").html();
// Set the HTML content of the first `div` element
$("#myDiv").html("<p>This is new HTML content</p>");
// Get the text content of the first `div` element
var text = $("#myDiv").text();
// Set the text content of the first `div` element
$("#myDiv").text("This is new text content");
As you can see, the .html() method returns or sets the entire HTML content of an element, while the .text() method only returns or sets the text content of an element.
Here is a table that summarizes the differences between the .html() and .text() methods:
Method
Description
.html()
Returns or sets the entire HTML content of an element, including any HTML tags.
.text()
Returns or sets the text content of an element, without any HTML tags.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The .html() and .text() methods in jQuery are used to get or set the contents of an HTML element. The main difference between the two methods is that .html() returns or sets the entire HTML content of an element, including any HTML tags, while .text() only returns or sets the text content of an element, without any HTML tags.
Here are some examples of how to use the .html() and .text() methods:
Code snippet
As you can see, the .html() method returns or sets the entire HTML content of an element, while the .text() method only returns or sets the text content of an element.
Here is a table that summarizes the differences between the .html() and .text() methods: