---
title: "Explain the difference between .html() and .text() in jQuery with examples."  
description: "Explain the difference between .html() and .text() in jQuery with examples."  
author: "Revati S Misra"  
published: 2023-04-14  
updated: 2023-06-12  
canonical: https://www.mindstick.com/forum/157820/explain-the-difference-between-html-and-text-in-jquery-with-examples  
category: "jquery"  
tags: ["jquery", "html", "jquery validate"]  
reading_time: 2 minutes  

---

# Explain the difference between .html() and .text() in jQuery with examples.

[Explain the difference](https://www.mindstick.com/forum/156125/can-you-explain-the-difference-between-organic-and-paid-results) between .html() and .text() in jQuery with examples.

## Replies

### Reply by Aryan Kumar

The .html() and .text() methods in jQuery are used to get or set the contents of an HTML element. The main [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) 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

```plaintext
// 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. |


---

Original Source: https://www.mindstick.com/forum/157820/explain-the-difference-between-html-and-text-in-jquery-with-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
