jQuery All Selector “*”

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

What Is the All Selector?

The all selector — written as "*" — matches every element in the current search context. It is the CSS universal selector, available since jQuery 1.0. Use it for demos and diagnostics; scope it for safer real-world use.

Remember
$("*")                    → every element in the document
$("#panel").find("*")     → every descendant inside #panel
$("body *")               → every descendant of body (not body itself)

$("*").length             → count matches
prefer $(".item") / $("#id") in production
UsePrefer
Whole document$("*") (demos only)
One container$("#box").find("*")
Known tag / class$("div") / $(".item")

How to Use the All Selector

Pass "*" to $(), optionally narrow the root, then read .length or chain a method.

  1. Decide the context — whole document, body, or a specific container.
  2. Call $("*") or $("#id").find("*").
  3. Use .length to count, or chain .css() / .each() on the set.
  4. On large pages, avoid global $("*") — scope first or pick a specific selector.
PieceRequired?Role
"*"YesUniversal selector string
Context / .find()OptionalLimits which subtree is searched
.lengthOptionalNumeric count of matched elements

Pseudocode

Pseudocode
root = document  (or a container element)
matches = empty list

for each element in root tree:
    if element is an element node:
        add element to matches

return jQuery(matches)
// then: .length | .css(...) | .each(...)

Cheat Sheet

Keep these facts handy while using "*".

TopicDetail
Syntax$("*") / jQuery("*")
Scoped$("#id").find("*")
Body only$("body *") (excludes <body> itself)
Count$("*").length
Nodes matchedElements only — not text or comments
Official cautionSlow except when used alone on small trees
SincejQuery 1.0 (CSS1 universal selector)

Live Preview

Compare how many elements match under different roots against a tiny sample DOM.

Sample markup mirrors the official jQuery all-selector demos (scoped under a container).

Result
$("#demo").find("*")
matched: 4
tags: DIV, SPAN, P, BUTTON

/* scoped universal — official pattern */

jQuery Examples

Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.

Example 1 — Count Every Element in the Document

Official idea: $("*") matches the whole document. Read .length to see how many elements exist.

jQuery
var elementCount = $("*").length;
console.log(elementCount + " elements found");
Try It Yourself

How It Works

  1. Run the universal selector. $("*") collects every element from the document root.
  2. Read the count. .length returns how many nodes matched — no loop needed.
  3. Expect a large number. Real pages include head tags, scripts, and chrome.
  4. Treat as approximate. Browser extensions can inject extra nodes and inflate the count.

Example 2 — Find All Elements Inside #test

Official scoped pattern: exclude the rest of the page with .find("*").

jQuery
var tags = $("#test").find("*").map(function () {
  return this.tagName;
}).get().join(", ");

console.log(tags);
console.log("count:", $("#test").find("*").length);
Try It Yourself

How It Works

  1. Pick a root. $("#test") selects one container.
  2. Walk descendants. .find("*") matches every element inside it — not #test itself.
  3. List tag names. .map() prints a clear sequence for learning.
  4. Why scope. Smaller trees are faster and ignore unrelated page chrome.

Example 3 — Count Without Changing Styles

Compare document-wide vs panel counts using .length alone.

jQuery
var totalInPage = $("*").length;
var totalInPanel = $("#test").find("*").length;

console.log("Document:", totalInPage);
console.log("#test panel:", totalInPanel);
Try It Yourself

How It Works

  1. Skip styling. You do not need .css() just to count nodes.
  2. Global count. $("*").length covers the whole document.
  3. Scoped count. .find("*").length covers one panel only.
  4. Compare. The gap shows how much scoping reduces the match set.

Example 4 — $("*") vs $("body *")

Limit to body descendants to skip head-only tags like title and meta.

jQuery
console.log("All nodes:", $("*").length);
console.log("Body descendants:", $("body *").length);

// body * does NOT include <body> itself — only children and below
Try It Yourself

How It Works

  1. Document root. $("*") starts at the whole document.
  2. Descendant combinator. $("body *") matches only nodes inside body.
  3. Body itself is excluded. The space combinator selects descendants, not the parent.
  4. Elements only. Text and comment nodes never match either selector.

Example 5 — Prefer a Specific Selector Over *

When you know the tag you need, skip the universal selector entirely.

jQuery
// Slow intent: collect everything, then filter
// $("*").filter("button")

// Fast intent: target the button directly
var label = $("#test button").text();
console.log(label);
Try It Yourself

How It Works

  1. Avoid collect-then-filter. $("*").filter(...) still visits every element first.
  2. Target directly. $("#test button") lets the engine skip unrelated nodes.
  3. Read the text. .text() returns the button label for a clear result.
  4. Production habit. Prefer ID, class, or tag selectors whenever you know the target.

Edge Cases & Pitfalls

Check these before using "*" in real projects.

slow

Global $("*") on large DOM

Official docs call it extremely slow except when used alone on small trees. Scope with .find("*") or use a specific selector.

count

Extensions inflate counts

Add-ons that inject nodes can raise $("*").length. Treat global counts as approximate.

filter

$("*").filter(...) still pays full cost

Filtering after a universal match still collects every element first. Prefer a direct selector.

nodes

Elements only

"*" never matches text or comment nodes. Use DOM APIs if you need those.

Browser Support

The all selector "*" is part of CSS1 and has worked in jQuery since 1.0. Any browser that runs your jQuery version supports it — performance, not compatibility, is the main concern.

✓ CSS1 · jQuery 1.0+

jQuery All Selector ("*")

Supported everywhere jQuery runs. Native equivalent: document.querySelectorAll("*"). Universal CSS selector.

100% Selector compatibility
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
"*" selector Universal

Bottom line: Compatibility is not the issue — scope your searches and prefer specific selectors in production. Use $("*") for small demos, diagnostics, or inside a narrow container.

Performance Notes

ApproachCostBest for
$("#box").find("*")Subtree sizeEvery descendant in one region
$(".item") / $("#id")Targeted lookupProduction UI code
$("*") on full pageWhole documentDemos / audits only

Official guidance: the universal selector is extremely slow except when used by itself. Combining * with other selectors forces more work. Keep the context small, or skip * when a tag, class, or ID will do.

Key Takeaways

  • Role: "*" matches every element in the search context.
  • Scope: prefer $("#id").find("*") over global $("*").
  • Count: use .length — no loop required.
  • Production: prefer ID, class, or tag selectors when you know the target.

One line: use "*" for universal matches — but keep the tree small, or pick a specific selector instead.

Frequently Asked Questions

The all selector matches every element node in the search context. $("*") from the document root collects html, head, body, and every descendant into one jQuery object you can chain with .css(), .each(), or .length.
Yes. jQuery uses the same idea as the CSS universal selector. The asterisk matches any element type.
jQuery must visit every element in the context. On a large page that can mean thousands of nodes. The official API warns it is extremely slow except when used by itself on a small tree.
Start from a container: $("#panel").find("*") or $("div.panel *"). Only descendants of that root are matched — fewer nodes and clearer intent.
Yes when searching from the document root. $("body *") skips head-only nodes like title and link, but still includes script tags inside body.
Read .length on the jQuery object: var n = $("#test").find("*").length. It returns a number, not a collection.

Next: :animated Selector

Learn how to select elements that are mid-animation with jQuery effects.

:animated 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.

9 people found this page helpful