Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 56 ยท Selecting Elements

Basic Selectors

Master jQuery's basic selectors: tag, id, class, groups, descendant, child and sibling combinators, attribute selectors, and how they map to querySelectorAll.

Every jQuery program starts by choosing which elements to work on. The tool for that is the selector: a string inside $() that describes the elements you want. If you already know CSS, you already know most of jQuery's selectors, because jQuery uses the same syntax.

In this lesson you will learn the standard selectors, how to combine them, and how they relate to the browser's native querySelectorAll. A later lesson covers jQuery's own extension selectors.

Tag, id, class and universal

The four simplest selectors identify elements by what they are or what they are called:

Selector Matches Example
"p" every <p> element $("p")
"#intro" the element with id="intro" $("#intro")
".note" every element with class note $(".note")
"*" every element $("*")

Ids should be unique in a page, so an id selector gives you at most one element. Classes can be shared by many elements, and an element can carry several classes: $(".note.urgent") (no space) matches elements that have both classes. You can also combine a tag with a class: $("li.done") matches list items with the class done, and ignores everything else with that class.

Tag, id and class selectors

The counts printed in the box show how many elements each selector matched. Notice that .note hits two paragraphs and the butter list item, while li.note narrows it down to the list item only.

Selecting several things at once

A comma joins independent selectors into a group. The result contains every element that matches any of them, in document order:

JavaScript
$("h1, h2, h3").addClass("heading");
$("#header, #footer").css("opacity", 0.8);

Each part is a full selector on its own, so "p.intro, ul li" means "paragraphs with class intro, plus all list items inside lists". If an element matches more than one part, it still appears only once.

Relationships between elements

A space or a symbol between two selectors describes how the elements are related in the tree. These are called combinators.

Syntax Name Meaning
A B descendant any B somewhere inside A, at any depth
A > B child B that is a direct child of A
A + B adjacent sibling the one B immediately after A
A ~ B general sibling every B after A, sharing the same parent

The difference between a descendant and a child selector is the one most people trip over. Given a nested menu, $("nav li") selects all list items at every level, while $("nav > ul > li") selects only the top-level ones.

Combinators in action

Only the three top-level items turn green, because > insists on a direct parent-child link. The descendant selector #menu li counts all five items.

Attribute selectors

You can select elements by their attributes, written in square brackets. These are standard CSS and work everywhere:

Selector Matches
[href] elements that have an href attribute at all
[type="email"] the attribute equals the value exactly
[href^="https"] the value starts with https
[href$=".pdf"] the value ends with .pdf
[class*="col-"] the value contains col- anywhere
[class~="tip"] a space-separated word list that contains the word tip
`[lang ="en"]`
[type!="hidden"] jQuery extension: does not equal (also matches elements without the attribute)

The four operators with symbols are easy to mix up. Think of ^ as the start of a regular expression, $ as the end, * as "anywhere", and ~ as "one of the words". The pipe in |= is the odd one out: it exists for hyphenated language codes. [a!=v] is not CSS, so it is slower, and :not([a="v"]) is the portable way to write it.

Attribute selectors are great for links and form fields. Combine them with a tag to be precise: $("a[href$='.pdf']") finds PDF links, and $("input[type='checkbox']") finds checkboxes. Quote the value when it contains characters other than letters and digits; when the selector is itself inside double quotes in JavaScript, use single quotes inside it, as in these examples.

Attribute selectors

How jQuery uses querySelectorAll

When you give $() a string, jQuery first checks for a few quick cases. A bare id such as "#intro" is answered with document.getElementById, and a plain tag or class can use getElementsByTagName or getElementsByClassName. For anything else that is valid CSS, jQuery calls the browser's native querySelectorAll, which is very fast.

When a selector contains syntax that is not valid CSS, such as :first or :contains() in the next lesson, querySelectorAll cannot understand it. jQuery then falls back to its own selector engine, called Sizzle, which is slower because it checks elements in JavaScript.

So the practical rule is: the standard selectors in this lesson are as fast as writing document.querySelectorAll yourself, and $("#id") is the fastest of all. In plain JavaScript terms, $(".note") and document.querySelectorAll(".note") select the same elements; the difference is that jQuery gives you a wrapper that understands implicit iteration and chaining.

Choosing a selector

p, li, a Select by tag name.
#id One unique element. The fastest.
.class Any number of elements.
A > B Combine with relationships.

Same syntax as CSS, so your CSS knowledge transfers directly.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which selector matches only the direct children of #menu that are list items?

    > is the child combinator. A space (descendant) matches list items at any depth.
  2. 2. What does $(".card.active") select?

    With no space between them, the two class selectors must both match the same element.
  3. 3. Which selector finds all links whose href ends with ".pdf"?

    $= means the value ends with the given text. ^= is starts with and *= is contains.
  4. 4. $("h1, h2, h3") selects...

    A comma makes a group: the result contains the elements that match any of the parts.

Summary

  • Selectors are CSS strings: "p", "#id", ".class", "*", and combinations like "li.done" or ".a.b".
  • Commas build groups; space, >, + and ~ express descendant, child, adjacent sibling and general sibling relationships.
  • Attribute selectors such as [type='email'], [href^='https'] and [href$='.pdf'] select by attribute values.
  • Standard CSS selectors are passed to querySelectorAll; #id is the quickest of all.
  • Non-CSS selectors fall back to the slower Sizzle engine, which the next lesson covers.

Next, let's explore jQuery's extra filtering selectors.