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
Call
What 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
Parameter
Description
Step 1
Start with an HTML string (fragment, template, or plain text).
Step 2
Optionally .trim() the string to drop edge whitespace text nodes.
Step 3
Call $.parseHTML(html) to get a plain array of nodes.
Return Value
Return Value
Description
Value / undefined
Getters 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"
Raw string. Spaces before/after the tag are kept by the HTML parser.
Without trim. Those spaces become #text nodes around STRONG.
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.