Back to Curriculum

jQuery Selectors

📚 Lesson 2 of 15 ⏱️ 30 min

jQuery Selectors

30 min

jQuery selectors allow you to find and select HTML elements based on their id, classes, types, attributes, values of attributes, and much more, enabling powerful element selection. Selectors are the foundation of jQuery—they find elements, then methods manipulate them. Understanding selectors enables effective jQuery usage. Selectors are essential for jQuery programming.

jQuery selectors are based on CSS selectors, so if you're familiar with CSS, you'll find jQuery selectors very familiar. jQuery extends CSS selectors with additional capabilities. Most CSS selectors work in jQuery. Understanding CSS selectors enables jQuery selection. Selector knowledge transfers from CSS to jQuery.

The most common selectors are element selectors (`$('p')`), id selectors (`$('#id')`), and class selectors (`$('.class')`). These basic selectors cover most use cases. Element selectors select by tag name. ID selectors select by id attribute. Class selectors select by class attribute. Understanding common selectors enables basic jQuery usage. Common selectors are fundamental.

jQuery also supports attribute selectors (`$('[attribute]')`), descendant selectors (`$('parent child')`), child selectors (`$('parent > child')`), and pseudo-selectors (`$(':first')`, `$(':last')`, `$(':even')`, `$(':odd')`). These advanced selectors enable complex selection. Understanding advanced selectors enables sophisticated element selection. Advanced selectors extend capabilities.

jQuery selectors return jQuery objects (not DOM elements), enabling method chaining. jQuery objects wrap DOM elements and provide jQuery methods. You can chain multiple methods on selected elements. Understanding jQuery objects enables effective chaining. jQuery objects are fundamental to jQuery.

Best practices include using specific selectors for performance, caching selectors when reusing them, understanding selector specificity, and using modern CSS selectors. Understanding jQuery selectors enables efficient element selection. Selectors are the foundation of jQuery.

Key Concepts

  • jQuery selectors allow finding and selecting HTML elements.
  • jQuery selectors are based on CSS selectors.
  • Common selectors: element, id, class selectors.
  • jQuery supports advanced selectors (attribute, descendant, pseudo-selectors).
  • jQuery selectors return jQuery objects enabling method chaining.

Learning Objectives

Master

  • Using basic selectors (element, id, class)
  • Using advanced selectors (attribute, descendant, pseudo)
  • Understanding selector specificity and performance
  • Caching selectors for reuse

Develop

  • Understanding CSS selector principles
  • Designing efficient element selection strategies
  • Appreciating selectors' role in jQuery

Tips

  • Use specific selectors for better performance: $('#id') is faster than $('.class').
  • Cache selectors when reusing: var $element = $('#myElement'); then use $element.
  • Use descendant selectors carefully—they can be slow on large DOMs.
  • Combine selectors: $('div.container p') selects paragraphs inside divs with class container.

Common Pitfalls

  • Using overly broad selectors, selecting unintended elements.
  • Not caching selectors, causing repeated DOM queries.
  • Using complex selectors when simple ones would work, reducing performance.
  • Not understanding selector specificity, getting unexpected results.

Summary

  • jQuery selectors allow finding and selecting HTML elements.
  • jQuery selectors are based on CSS selectors.
  • Common selectors cover most use cases.
  • Advanced selectors enable complex element selection.
  • Understanding selectors enables effective jQuery usage.

Exercise

Use different jQuery selectors to select and style elements.

// Select by element
$('p').css('color', 'blue');

// Select by ID
$('#myId').css('background-color', 'yellow');

// Select by class
$('.myClass').css('font-size', '18px');

// Select by attribute
$('input[type="text"]').css('border', '2px solid red');

Code Editor

Output