Chapter 3. Selectors and filters

 

This chapter covers

  • Existing types of jQuery selectors
  • Adding your own pseudo-class selectors
  • Adding set filters

One of the principles behind jQuery is the select-and-act mode of operation—you select one or more elements of interest and then do something with them. jQuery includes an embedded copy of the Sizzle selector engine (“a pure JavaScript CSS selector engine designed to be easily dropped into a host library”; http://sizzlejs.com/) to perform your selections. Internally, Sizzle delegates to built-in browser functionality when available, and it applies the selectors itself in JavaScript if necessary.

Although numerous built-in selectors are available, sometimes it’s cleaner and clearer to create a custom selector that you can integrate with jQuery’s other abilities. Fortunately, adding a new selector is one of the easiest enhancements you can make to jQuery.

Note

Although initially developed as part of jQuery, the Sizzle selector engine has been released as a separate project for use with any JavaScript library. To enable development by a wider community, the Sizzle code has been turned over to the Dojo Foundation (http://dojofoundation.org/; not to be confused with the Dojo Toolkit). Interest has come from other JavaScript communities, with the aim of enhancing the engine for everyone’s benefit.

3.1. What are selectors and filters?

3.2. Adding a pseudo-class selector

3.3. Adding a set filter

3.4. Summary