Lesson 3: jQuery Selectors
Master the powerful jQuery selectors - the essential tools for targeting and manipulating DOM elements with precision and efficiency.
Understanding jQuery Selectors
jQuery selectors are the foundation of the jQuery library. They allow you to select and manipulate HTML elements using CSS-style syntax, making DOM traversal and manipulation incredibly efficient.
- Use familiar CSS syntax to target elements
- Simplify complex DOM traversal tasks
- Work consistently across all browsers
- Enable efficient element manipulation
- Support advanced filtering and chaining
The Basic Selector Syntax
jQuery uses the same selectors as CSS to target elements:
Element Selector
$("element")
Selects all elements of a specific type
ID Selector
$("#id")
Selects a single element with a specific ID
Class Selector
$(".class")
Selects all elements with a specific class
Basic Selectors in Action
Try these interactive demos to see how basic selectors work:
Element Selector
Select and manipulate all elements of a specific type:
// Select all paragraph elements $("#selectParagraphs").click(function() { $("[data-type='paragraph']").addClass("highlight"); }); // Select all heading elements $("#selectHeadings").click(function() { $("[data-type='heading']").addClass("highlight"); }); // Reset all $("#resetAll").click(function() { $(".demo-item").removeClass("highlight"); });
ID Selector
Target a specific element using its unique ID:
// Select element with ID specialBox1 $("#selectBox1").click(function() { $("#specialBox1").addClass("highlight"); }); // Select element with ID specialBox4 $("#selectBox4").click(function() { $("#specialBox4").addClass("highlight"); });
Class Selector
Select multiple elements sharing the same class:
// Select all elements with class 'important' $("#selectImportant").click(function() { $(".important").addClass("highlight"); }); // Reset selection $("#resetClass").click(function() { $(".demo-item").removeClass("highlight"); });
Advanced Selectors
jQuery provides powerful selectors beyond the basics:
Selector Type | Syntax | Description | Example |
---|---|---|---|
Attribute Selector | $("[attribute]") |
Selects elements with specified attribute | $("[data-info]") |
Multiple Selector | $("sel1, sel2") |
Selects multiple elements at once | $("h1, h2, h3") |
Descendant Selector | $("ancestor descendant") |
Selects descendants of an element | $("div p") |
Child Selector | $("parent > child") |
Selects direct children only | $("ul > li") |
First/Last Selector | $(":first"), $(":last") |
Selects the first/last element | $("p:first") |
Attribute Selector Demo
Select elements based on their attributes:
// Select elements with data-role="admin" $("#selectAdmins").click(function() { $("[data-role='admin']").addClass("highlight"); }); // Select elements with data-role="user" $("#selectUsers").click(function() { $("[data-role='user']").addClass("highlight"); }); // Select elements with data-role="guest" $("#selectGuests").click(function() { $("[data-role='guest']").addClass("highlight"); });
Selector Best Practices
Follow these guidelines to write efficient jQuery code:
Optimize Performance
ID selectors are fastest. Use them whenever possible for better performance.
$("#myElement")
Be Specific
Combine selectors to target elements more precisely.
$("ul.nav > li")
Cache Selectors
Store frequently used selectors in variables.
var $nav = $(".main-nav");
Context Matters
Provide context to limit search scope and improve performance.
$("li", "#myList")