jQuery Element Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

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

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

// Native twin: document.getElementsByTagName("div")
CallWhat it does
ElementLearn the jQuery element selector: match HTML tags with $(\

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Pass the tag name: $("div"), $("li"), $("input").
Step 2Combine with a class or id when you need fewer matches: $("div.note").
Step 3Scope under a container on large pages: $("#panel p").

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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 ("#id")

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

ID Selector ("#id") 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