From the course: jQuery Essential Training

Overview of selectors and filters - jQuery Tutorial

From the course: jQuery Essential Training

Start my 1-month free trial

Overview of selectors and filters

- As I talked about earlier, retrieving content from a page is a very basic and common web development pattern. This is where jQuery selectors and filters come in handy. It's one of the main things that jQuery makes a lot easier to accomplish with a lot less code, and that's what we're going to look at in this chapter. Selectors and filters work together to retrieve document content. Selectors are used to select content, as their name implies, and then filters are used to further refine the selected content. In this way, you can think of selectors and filters as the query part of jQuery. The resulting content can then be manipulated by other jQuery or plain JavaScript functions. The result of using selectors and filters to retrieve content, is an array of objects that meet the specified criteria. Now it's important to understand that this array that comes back is not a set of pure DOM elements. It's a collection of jQuery objects that are wrapped around each of the DOM elements. These jQuery objects provide a large number of functions and properties for further operating on the content. Of course, you can get access to the underlying DOM element for each one of these objects if you want to. But the whole purpose of using jQuery objects is that you have a whole bunch of convenience functions and high level ways of operating on them without having to resort to the DOM. Let's consider a conceptual example. Suppose I have a web application that presents a gallery of photos. I might want to do some processing of all the photo elements in the page. For example, add event handlers or sort them, or perform some other operation. Selectors make this easy by allowing me to specify a simple expression that returns all the images in the page, which I can then manipulate. Of course, the best way to learn about how selectors and filters work is to actually see them in action. In the rest of this chapter, we'll see how selectors and filters work together to retrieve content based on different kinds of criteria.

Contents