How to Decode HTML Entities Using jQuery?

Learn how to decode HTML entities in jQuery using smart and efficient methods. Includes working examples and cross-browser techniques.

When working with dynamic HTML content, you might encounter special characters represented as HTML entities (e.g., &lt; for <, &gt; for >). These are essential for rendering text safely in browsers, but sometimes you may need to convert them back to their original characters — a process known as decoding.

In this article, we’ll explore how to decode HTML entities using jQuery in a simple and effective way.

Why Decode HTML Entities?

Most of the browsers convert certain characters into HTML entities to avoid rendering issues or XSS attacks. For example:

  • < becomes &lt;
  • > becomes &gt;
  • & becomes &amp;
  • " becomes &quot;

If you’re extracting or manipulating HTML content through JavaScript or AJAX, you may receive entity-encoded strings. To properly display or process them, you’ll need to decode these entities to a readable format.

jQuery Method to Decode HTML Entities

jQuery itself doesn’t provide a direct function to decode HTML entities, but you can use the browser’s native HTML parsing capability by leveraging a temporary DOM element like textarea.

Example Code:

function decodeHtmlEntities(encodedStr) {
    return $('<textarea/>').html(encodedStr).text();
}

// Usage
var encoded = '&lt;p&gt;Hello World!&lt;/p&gt;';
var decoded = decodeHtmlEntities(encoded);

console.log(decoded); // Output: <p>Hello World!</p>

Explanation:

  • $('<textarea/>') creates a temporary <textarea> element.
  • .html(encodedStr) sets the HTML content with entities.
  • .text() extracts the decoded plain text.

This approach is safe and works across all modern browsers.

Alternative Vanilla JavaScript Method

If you prefer using plain JavaScript without jQuery, here’s an equivalent solution:

function decodeHtmlEntities(encodedStr) {
    var txt = document.createElement('textarea');
    txt.innerHTML = encodedStr;
    return txt.value;
}

// Usage
var decoded = decodeHtmlEntities('&amp;copy; 2025');
console.log(decoded); // Output: © 2025

This is same example in Vanilla Javascript.

Use Case Examples

Below are some examples to demostrate the usage of decodeHtmlEntities function.

Example 1: Decoding API Response

If your API is giving response in HTML entities, you can use this function to decode it.

$.get('/get-description', function(data) {
    var decoded = decodeHtmlEntities(data.description);
    $('#description').text(decoded);
});

Example 2: Decoding User Input Stored with Entities

Sometimes, we store user input data in the database with HTML encoded string. We can use this function to decode HTML entities.

var userInput = '&lt;script&gt;alert(&quot;Hi&quot;)&lt;/script&gt;';
$('#output').text(decodeHtmlEntities(userInput));

Conclusion

Decoding HTML entities is essential when you’re dealing with encoded data from the server or other sources. Using jQuery’s ability to manipulate DOM elements, you can easily decode entities without additional libraries.

This simple technique ensures that your content is displayed correctly, improving both functionality and user experience.