jQuery.htmlPrefilter( html ) (alias $.htmlPrefilter) is a hook that jQuery runs on HTML strings right before its manipulation methods — .html(), .append(), $("<p>…</p>") and friends — hand them to the browser parser (added in jQuery 1.12 / 2.2). Since jQuery 3.5.0 the default returns the string unchanged. You may replace it, but do so with great care.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$.htmlPrefilter(html) → String
// Default in jQuery 3.5+: passthrough
$.htmlPrefilter("<p>Hi</p>") // "<p>Hi</p>"
// Override (advanced) - always call through unless you know why not
var original = $.htmlPrefilter;
$.htmlPrefilter = function (html) {
return original.call(this, html);
};
Call
What it does
$.htmlPrefilter(html)
Run the current filter on an HTML string and get a string back.
$.htmlPrefilter = function
Replace the filter — every later manipulation call goes through it.
.html(str) / .append(str)
Manipulation methods that call the filter for markup strings.
$("<p>x</p>")
Creating elements from a complex HTML string also passes through the filter.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
html
String — the HTML string about to be parsed. When you override the hook, your function receives this string and must return a string.
this (when overriding)
Use original.call(this, html) to hand off to the previous filter so nothing is lost.
Return Value
Return Value
Description
String
The HTML string after filtering. The default filter in jQuery 3.5+ returns exactly the same string it received.
jQuery Examples
Example 1 — Default Is a Passthrough
In jQuery 3.5+ the filter returns the very same string — even self-closing tags stay untouched.
jQuery
var html = "<div/><span>Hi</span>";
var result = $.htmlPrefilter(html);
console.log(result); // "<div/><span>Hi</span>"
console.log(result === html); // true
console.log(typeof $.htmlPrefilter); // "function"
Modern default. HTML ignores the slash on <span/>, so the text lands inside the span.
Custom filter. The regex turns <span/> into <span></span> before parsing.
Result. The same input now produces a different DOM — proof the hook runs first.
Frequently Asked Questions
It is a hook jQuery calls on HTML strings before its manipulation methods parse them into DOM nodes. .html(), .append(), .prepend(), .before(), .after(), .replaceWith() and $(html) all run complex markup through it. In jQuery 3.5 and later the default simply returns the string unchanged.
Earlier versions rewrote self-closing tags such as <div/> into <div></div> using a regular expression. That conversion made certain XSS attacks possible, so jQuery 3.5.0 made htmlPrefilter an identity function. Code that relied on the old expansion should use properly closed tags like <div></div>.
No. It is a low-level hook, not a sanitizer. Never rely on it to clean untrusted HTML. Sanitize user-supplied markup with a dedicated library such as DOMPurify, or insert it as text with .text() instead of .html().
Rarely. Overriding changes how every jQuery HTML insertion behaves on the page, including plugins. If you must, save the original, call it from your filter, and keep your transformation minimal and well tested.
Methods that turn an HTML string into nodes: $(htmlString), .html(string), .append(), .prepend(), .before(), .after(), .wrap(), .replaceWith() and similar. Simple single tags like $("<div>") are created directly and skip the filter.
parseHTML() is a public method you call yourself to turn a string into an array of DOM nodes. htmlPrefilter() is a hook that runs inside jQuery's own manipulation code, before parsing, and returns another string.