jQuery parseHTML() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.parseHTML(data [, context ] [, keepScripts ]) (alias $.parseHTML) turns an HTML string into an array of DOM nodes using native browser parsing (added in jQuery 1.8). Nodes stay detached until you append them.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.parseHTML(htmlString)  → Array of DOM nodes

nodes = $.parseHTML("<p>Hi</p><span>Go</span>")
// → [ <p>, <span> ]

$("#box").append(nodes)   // now visible on the page
CallWhat it does
parseHTML()Learn jQuery.parseHTML(): turn HTML strings into DOM node arrays, trim whitespace, keepScripts, jQuery 3.0 context, XSS notes, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with an HTML string (fragment, template, or plain text).
Step 2Optionally .trim() the string to drop edge whitespace text nodes.
Step 3Call $.parseHTML(html) to get a plain array of nodes.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official Demo (Plain Text)

Official jQuery API pattern: even plain text becomes a node.

jQuery
var str = "hello, my name is jQuery.";
var html = $.parseHTML(str);
var names = [];

$.each(html, function (i, el) {
  names.push(el.nodeName);
});

console.log(html.length);       // 1
console.log(names.join(", "));  // "#text"
Try It Yourself

How It Works

  1. Input. The string has no tags — it is still valid input for parseHTML.
  2. Parse. $.parseHTML(str) returns an array with one text node.
  3. Inspect. el.nodeName for a text node is "#text".

Example 2 — Parse an HTML Fragment

Turn a small markup string into real elements, then mount them.

jQuery
var markup = "<p>Hello</p><span>World</span>";
var nodes = $.parseHTML(markup);
var names = $.map(nodes, function (el) { return el.nodeName; });

console.log(names.join(" → "));  // "P → SPAN"
$("#mount").append(nodes);
Try It Yourself

How It Works

  1. Fragment. Two sibling tags become two top-level nodes in the array.
  2. Map. $.map collects each nodeName (P, SPAN).
  3. Append. .append(nodes) inserts the array into #mount.

Example 3 — Trim Edge Whitespace

Leading/trailing spaces can become extra text nodes.

jQuery
var raw = "   <strong>Bold</strong>   ";
var withoutTrim = $.parseHTML(raw);
var withTrim = $.parseHTML(raw.trim());

console.log(withoutTrim.length);  // 3  (#text, STRONG, #text)
console.log(withTrim.length);     // 1  (STRONG only)
Try It Yourself

How It Works

  1. Raw string. Spaces before/after the tag are kept by the HTML parser.
  2. Without trim. Those spaces become #text nodes around STRONG.
  3. With trim. raw.trim() removes edge spaces first — one element remains.

Frequently Asked Questions

jQuery.parseHTML(data, context, keepScripts) parses an HTML string and returns an array of DOM nodes — elements, text nodes, and comments — without inserting them into the page. Append the array with .append() or inspect nodes first.
The jQuery API passes a plain text string to $.parseHTML(), appends the resulting nodes to a log div, and lists each node name (such as #text). Even a string without tags becomes a text node in the returned array.
parseHTML returns a plain array of nodes and does not run scripts by default (keepScripts is false). $(html) builds a jQuery collection and inserting that HTML into the live document can still run malicious code. Use parseHTML when you want nodes first, then decide what to inject.
When keepScripts is false (the default), script elements in the HTML string are omitted from the returned nodes. When true, script tags are included in the array. Never pass untrusted HTML with keepScripts true.
Leading and trailing whitespace in the HTML string can become text nodes. The jQuery API recommends jQuery.trim() (or String.prototype.trim()) first when you want to avoid extra edge whitespace nodes.
When context is omitted, null, or undefined, jQuery 3.0+ uses a new detached document to parse HTML. That helps prevent inline event handlers from firing during parsing. Once you inject the nodes into the live page, handlers can still run — always sanitize untrusted input.
No. It is not a security filter. Indirect script execution (for example via event-handler attributes) is still possible. Clean or escape untrusted input from URLs, cookies, or forms before parsing and inserting.

Next: htmlPrefilter() Method

Learn the hook jQuery runs on HTML strings before it parses them.

htmlPrefilter() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful