jQuery :header Selector

Beginner
5 min read
Updated: Oct 2026
5 examples
Live preview

What Is :header?

:header (since jQuery 1.2) selects all heading elements — h1 through h6 — in one expression. It matches real heading tags only, not “title-looking” div or p elements.

Remember
$(":header")                         // all h1–h6 on the page
$("article :header")                 // headings inside article only
$("#main *").filter(":header")       // faster scoped pattern

// Same matches as:
$("h1, h2, h3, h4, h5, h6")
NeedUse
All heading tags$(":header")
One heading level$("h2")
CSS-native list$("h1, h2, h3, h4, h5, h6")
Parents that contain something$(":has(…)")
Text match$(":contains(…)")

How to Use :header

Pick headings with $(":header"), then style, count, or build a TOC from the set.

  1. Call $(":header") (or scope it: $("article :header")).
  2. jQuery returns every h1–h6 in that context.
  3. Chain methods — .css(), .each(), .text(), and so on.
  4. On large pages, prefer $("#scope *").filter(":header").
  5. Remember: classed “titles” that are not heading tags are ignored.
PieceRequired?Role
:headerYesMatch h1–h6
Context / prefixNoLimit search (e.g. article :header)
.filter(":header")NoRecommended performance pattern
Return value—jQuery set of heading elements

Pseudocode

Pseudocode
call $(":header")   // or $("context :header")

for each element in the document (or context):
    if tagName is h1, h2, h3, h4, h5, or h6:
        include it in the result set

return the jQuery set of those headings

Cheat Sheet

Keep these facts handy when selecting headings.

TopicDetail
Syntax$(":header") · $("context :header")
SincejQuery 1.2
Matchesh1, h2, h3, h4, h5, h6
CSS?No — jQuery extension
CSS equivalenth1, h2, h3, h4, h5, h6
Faster pattern$("#el *").filter(":header")
Does not matchStyled div / p “titles”

Live Preview

Click Run to list every :header match in the sample markup below.

Guide

Intro paragraph (not a header).

Chapter One

Details

Fake title class — ignored

Only real h1–h6 tags match. The paragraph with class title is skipped.

Result
Ready — click Run to list headings.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Select All Headings

Official idea: match every h1–h6 and mark them.

jQuery
$(":header").each(function () {
  $(this).append(" [header]");
});
Try It Yourself

How It Works

  1. Select. $(":header") finds all heading tags.
  2. Skip. Paragraphs and other tags are not included.
  3. Each. Loop marks every matched heading.
  4. Levels. Works for h1 through h6 the same way.

Example 2 — Performance: .filter(":header")

Official tip: CSS-select a region first, then filter headings.

jQuery
$("#content *").filter(":header").each(function () {
  $(this).append(" [in content]");
});
Try It Yourself

How It Works

  1. CSS first. $("#content *") uses a fast native path.
  2. Filter. .filter(":header") keeps only heading tags.
  3. Scope. Sidebar headings are outside #content.
  4. Why. Avoids a full-page jQuery-extension scan.

Example 3 — Scoped: article :header

Limit matches to headings inside article only.

jQuery
$("article :header").each(function () {
  $(this).append(" [article]");
});
Try It Yourself

How It Works

  1. Descendant. Space means “inside article”.
  2. Match. Only headings nested under article.
  3. Aside. Sibling regions are ignored.
  4. Use. Great for styling or outlining one document region.

Example 4 — CSS Equivalent Tag List

:header and h1, h2, …, h6 match the same tags.

jQuery
var a = $(":header").length;
var b = $("h1, h2, h3, h4, h5, h6").length;
$("#log").text(":header → " + a + " | tag list → " + b);
Try It Yourself

How It Works

  1. Same tags. Both selectors target h1–h6.
  2. Count. Lengths should match on the same DOM.
  3. Trade-off. Tag list is CSS-native; :header is shorter to type.
  4. Pick. Use the tag list when you need native selector speed.

Example 5 — Build a Table of Contents

Walk every heading and prepend a simple outline of links.

jQuery
var $list = $("<ul id='toc'></ul>");
$(":header").each(function (i) {
  var id = "section-" + i;
  $(this).attr("id", id);
  $list.append(
    $("<li><a></a></li>")
      .find("a")
      .attr("href", "#" + id)
      .text($(this).text())
      .end()
  );
});
$("body").prepend($list);
Try It Yourself

How It Works

  1. Collect. $(":header") finds every heading.
  2. Ids. Each heading gets a unique id.
  3. Links. TOC entries point to those ids.
  4. Prepend. The list is inserted at the top of body.

Edge Cases & Pitfalls

Watch these when heading counts look “wrong.”

tags

Only real h1–h6 tags

A div.title or bold p never matches, no matter how it looks.

perf

Bare $(":header") can be slow

On big pages, scope first: $("#main *").filter(":header") or use h1, h2, …, h6.

scope

Page-wide by default

Without a context, you also pick nav/footer headings. Scope when you mean “article only.”

css

Not valid in plain CSS files

:header works in jQuery only. Stylesheets need h1, h2, h3, h4, h5, h6.

Browser Support

:header has been part of jQuery since 1.2. It works in every browser that runs your loaded jQuery version — it is implemented by jQuery, not by the browser’s CSS engine.

✓ Stable · jQuery 1.2+

jQuery :header

Supported across jQuery 1.x, 2.x, and 3.x. Prefer a current jQuery 3.x build. For native CSS speed, use h1–h6 lists instead.

100% With jQuery
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:header Universal

Bottom line: Safe everywhere jQuery runs. Remember it is a jQuery extension — scope or filter for best performance on large DOMs.

Performance Notes

ApproachCostBest for
$("h1, h2, h3, h4, h5, h6")FastestNative CSS path
$("#el *").filter(":header")FastScoped jQuery-extension use
$(":header") page-wideSlowerSmall pages / quick demos

Official guidance: select with pure CSS first, then .filter(":header"), so you keep the convenience without a full-page extension scan.

Key Takeaways

  • Role: one selector for all h1–h6 tags — since jQuery 1.2.
  • Tags only: ignores styled non-heading elements.
  • Scope: $("article :header") or .filter(":header").
  • Extension: not CSS — prefer tag lists when speed matters.

One line: :header grabs every real heading tag — short to write, scope or filter when the page is large.

Frequently Asked Questions

:header selects every heading element — h1, h2, h3, h4, h5, and h6. One pseudo-class replaces writing h1, h2, h3, h4, h5, h6 when you need all heading levels. Available since jQuery 1.2.
No. :header only matches real h1–h6 tags. A div with class "title" or a bold paragraph is not a header unless it uses a heading tag.
No. It is a jQuery extension and cannot use the native querySelectorAll() boost. The CSS-equivalent selector is h1, h2, h3, h4, h5, h6.
Official docs recommend a pure CSS select first, then .filter(":header"). Example: $("#main *").filter(":header") instead of bare $(":header") on large pages when you only care about one region.
Yes. Prefix with a container — $("article :header") matches only headings inside article elements.
:header matches element type (heading tags). :contains() matches text substrings. :has() matches parents that contain a descendant. They solve different problems.

Next: :hidden Selector

Select elements that jQuery considers hidden — display, visibility, and more.

:hidden tutorial →

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.

10 people found this page helpful