jQuery Element Selector

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

What Is the Element Selector?

The element selector $("element") (since jQuery 1.0) selects every node with that HTML tag name — the same idea as a CSS type selector. Official docs note jQuery uses native getElementsByTagName() for this expression.

Remember
$("div")        // every <div>
$("p")          // every <p>
$("span")       // every <span>

$("div.card")   // div + class
$("#box div")   // divs under #box only

// Native twin: document.getElementsByTagName("div")
NeedUse
All tags of one type$("div")
Tag + class$("div.card")
One element by id$("#id")
Scoped tags$("#main p")
Several tags$("div, span")
Everything$("*")

How to Use the Element Selector

Write the tag name in lowercase, then narrow with class, id, or a parent when needed.

  1. Pass the tag name: $("div"), $("li"), $("input").
  2. Combine with a class or id when you need fewer matches: $("div.note").
  3. Scope under a container on large pages: $("#panel p").
  4. Use commas for several tags at once: $("h1, h2, h3").
  5. Prefer lowercase tag names for consistency across HTML/XML contexts.
PieceRequired?Role
Tag nameYesMatches element.tagName (e.g. div)
Class / id / ancestorOptionalNarrows which tags are returned
Native path—getElementsByTagName() for bare tags
Return value—jQuery collection of matching elements

Pseudocode

Pseudocode
matches = getElementsByTagName(tagName)   // native when bare

// or, conceptually:
matches = []
for each element in document (or context):
    if element.tagName equals tagName:
        matches.add(element)

return jQuery(matches)

Cheat Sheet

Keep these facts handy while selecting by tag name.

TopicDetail
Syntax$("tagName")
SincejQuery 1.0 · CSS type selector
Native APIgetElementsByTagName()
Case habitUse lowercase tag names
+ class$("div.card")
Scoped$("#box span")
Multiple tags$("div, p, span")
Empty setNo such tags → length 0

Live Preview

Run $("div"), $("span"), or both against the official-style sample boxes.

Sample markup

DIV1
DIV2
SPAN

Same idea as the official jQuery demo — tag name only, no class required.

Result
$("div").length
→ 2

matched → DIV1, DIV2

jQuery Examples

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

Example 1 — Official $("div")

Select every div — spans and other tags are skipped.

jQuery
var labels = [];
$("div").each(function () {
  labels.push($.trim($(this).text()));
});

$("#out").text(
  "Matched divs: " + labels.length + "\n" +
  labels.join(", ")
);
Try It Yourself

How It Works

  1. Query. $("div") asks for every div tag.
  2. Native. jQuery uses getElementsByTagName("div") under the hood.
  3. Skip. The span is a different tag name.
  4. List. We print each div’s text so you can see the matches.

Example 2 — Count Different Tags

Compare how many div, p, and span elements exist.

jQuery
$("#out").text(
  "div: " + $("div").length + "\n" +
  "p: " + $("p").length + "\n" +
  "span: " + $("span").length
);
Try It Yourself

How It Works

  1. Separate queries. Each tag name is its own selector.
  2. Length. .length is the match count.
  3. Independent. Counting div does not affect p.
  4. Use. Quick inventory before styling or binding events.

Example 3 — Tag + Class

Narrow a tag selector with a class so other divs stay out.

jQuery
$("#out").text(
  "All div: " + $("div").length + "\n" +
  "div.card: " + $("div.card").length
);
Try It Yourself

How It Works

  1. Broad. $("div") finds every div.
  2. Narrow. $("div.card") keeps only divs with class card.
  3. Order. Tag first, then class — standard CSS compound selector.
  4. Why. Safer than styling every div on the page.

Example 4 — Scoped Under a Parent

Select paragraphs only inside one container.

jQuery
$("#out").text(
  "Inside #main: " + $("#main p").length + "\n" +
  "Whole page p: " + $("p").length
);
Try It Yourself

How It Works

  1. Scope. #main p is a descendant + element combo.
  2. Local. Only paragraphs under #main match.
  3. Global. Bare $("p") also finds outside paragraphs.
  4. Habit. Scope tag selectors on real multi-section pages.

Example 5 — Multiple Tag Names

Select several element types in one query with commas.

jQuery
var labels = [];
$("h1, h2, h3").each(function () {
  labels.push(this.tagName.toLowerCase() + ": " + $.trim($(this).text()));
});

$("#out").text(labels.join("\n"));
Try It Yourself

How It Works

  1. Comma. $("h1, h2, h3") is the multiple selector.
  2. Union. Each heading level is included in one collection.
  3. Order. Matches usually appear in document order.
  4. Next. See the multiple-selector tutorial for more patterns.

Edge Cases & Pitfalls

Check these before selecting by tag name in real pages.

scope

Bare $("div") is page-wide

It matches layout wrappers, ads, and widgets too. Prefer a parent id/class or a compound selector.

case

Prefer lowercase tags

HTML is usually case-insensitive, but lowercase ($("div")) is the safe, conventional form.

custom

Custom elements work too

$("my-widget") matches custom tag names the same way — still prefer scoping on large apps.

live

Selection is a snapshot

New tags added later are not in an old jQuery set. Re-run the selector or use delegated events.

Browser Support

The element (type) selector is standard CSS and has been in jQuery since 1.0. Bare tag queries use native getElementsByTagName().

✓ Stable · CSS · jQuery 1.0+

jQuery element selector

Supported across jQuery 1.x, 2.x, and 3.x in every browser that runs your jQuery build. Prefer current jQuery 3.x for modern projects.

100% Type selector support
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
$("element") Universal

Bottom line: Safe everywhere jQuery runs. Scope or combine with class/id when you do not mean every tag on the page.

Performance Notes

ApproachCostBest for
$("div")Cheap (native)Simple tag lookups
$("#id div")Cheaper scopedLarge pages
$("div.card")CheapTag + class filters
$("*")ExpensiveAvoid unless you need every node

Tag selectors are fast because browsers optimize getElementsByTagName. Cost grows when the match set is huge — scope early when possible.

Key Takeaways

  • Role: $("tag") matches every element with that tag name.
  • Native: bare tags use getElementsByTagName().
  • Narrow: combine with class, id, or a parent context.
  • Multi: use $("div, span") for several tags.

One line: $("element") selects by tag name — fast and foundational, best when scoped or combined.

Frequently Asked Questions

The element selector matches every DOM node with a given HTML tag name. $("div") selects all div elements; $("p") selects all paragraphs. Available since jQuery 1.0 — the same idea as CSS type selectors.
Conceptually yes. Official jQuery docs state that getElementsByTagName() is used for a bare element selector. jQuery wraps the result so you can chain .css(), .on(), and other methods.
In HTML, tag matching is case-insensitive in the DOM, but convention is lowercase: $("div"), $("span"), $("input"). Lowercase avoids surprises in XML/XHTML documents.
Element selects by tag type. Class (.card) and ID (#header) narrow by attributes. Combine them: $("div.card") means div elements that also have class card.
Often no. Prefer scoping: $("#main div") or $("div.card"). A bare $("div") touches every div in the document and can over-style or over-bind.
Yes. It is the CSS type selector and has been in jQuery since 1.0. It works in native querySelectorAll as well when you need non-jQuery code.

Next: ID Selector

Learn how to select a single element uniquely with $("#id").

ID selector 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.

8 people found this page helpful