jQuery htmlPrefilter() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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);
};
CallWhat it does
$.htmlPrefilter(html)Run the current filter on an HTML string and get a string back.
$.htmlPrefilter = functionReplace 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

ParameterDescription
htmlString — 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 ValueDescription
StringThe 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"
Try It Yourself

How It Works

  1. Input. A string with a self-closing <div/> and a normal span.
  2. Filter. $.htmlPrefilter(html) runs the default filter.
  3. Compare. The result is identical — modern jQuery no longer rewrites the markup.

Example 2 — Log Every HTML String

Wrap the original filter to see which strings jQuery passes through, then hand them on untouched.

jQuery
var original = $.htmlPrefilter;
var seen = [];

$.htmlPrefilter = function (html) {
  seen.push(html);
  return original.call(this, html);   // keep default behavior
};

$("#box").append("<p>Hello</p>");
$("#box").html("<b>Bold</b>");

console.log(seen.length);       // 2
console.log(seen.join(" | "));  // "<p>Hello</p> | <b>Bold</b>"
Try It Yourself

How It Works

  1. Save. Keep a reference to the original filter before replacing it.
  2. Wrap. The new function records the string and returns original.call(this, html).
  3. Trigger. .append() and .html() each send their markup through the hook.

Example 3 — Restore Old Self-Closing Behavior

The documented use case: bring back the pre-3.5 <span/> expansion for legacy markup. Demo only — that behavior was removed for security.

jQuery
var rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([a-z][^\/\0>\x20\t\r\n\f]*)[^>]*)\/>/gi;

$("#modern").html("<div><span/>text</div>");
console.log($("#modern").html());
// "<div><span>text</span></div>"   (span swallowed the text)

$.htmlPrefilter = function (html) {
  return html.replace(rxhtmlTag, "<$1></$2>");
};

$("#legacy").html("<div><span/>text</div>");
console.log($("#legacy").html());
// "<div><span></span>text</div>"   (span closed, text is a sibling)
Try It Yourself

How It Works

  1. Modern default. HTML ignores the slash on <span/>, so the text lands inside the span.
  2. Custom filter. The regex turns <span/> into <span></span> before parsing.
  3. 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.

Next: parseXML() Method

Learn how jQuery parses a well-formed XML string into an XMLDocument.

parseXML() 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.

8 people found this page helpful