Page 1 of 3
Selectors are what jQuery uses to pick out particular objects in the DOM. While this might start out simply enough, it can appear to be complicated in more testing examples. The trick is to always remember what the selector is doing.
Now Available as a Print Book:
You can buy it from:
USA and World Amazon.com
I The Core UI
- Understanding jQuery (Book Only)
- The DOM And Selectors
- CSS Selectors
- The jQuery Object (Book Only)
- Advanced Filters - Traversing The DOM
- Modifying DOM Objects
- Creating And Modifying The DOM
- jQuery Data
- Function Queues
- jQuery UI
- jQuery UI Custom Control - Widget Factory
- jQuery - Easy Plugins
- Getting Started With QUnit Testing
The companion volume will be available as a book very soon:
Just jQuery: Events, Async & Ajax
- Events In jQuery (coming soon)
- Promises & Deferred
- Using Promises
- Ajax the Basics - get
- Ajax the Basics - post
- Ajax - Advanced Ajax To The Server
- Ajax - Advanced Ajax To The Client
- Ajax - Advanced Ajax Transports And JSONP
- Ajax - Advanced Ajax The jsXHR Object
- Ajax - Advanced Ajax Character Coding And Encoding
In the first chapter we introduced the idea that one of the big uses of jQuery is in finding sets of DOM objects so that we can work with them.
There are two phases to learning the core of jQuery. The first is learning how to pick out the elements that you want. The second is finding out how to make the changes that you require.
In this article we focus on finding elements, leaving manipulation to another chapter. Of course, jQuery isn't of much use if all you can do is find elements, but it is the first, and necessary, step.
In an ideal world any element, or its corresponding DOM object, would be easy to find because it would have a unique id. Even sets of DOM objects that share a set of common properties should be easy to find because they should all belong to the same class.
Before going any deeper, it is worth saying that the three most used jQuery selectors are
- name all tags like <name>
- #name the singe tag with id="name"
- .name all tags with class="name"
These were introduced in the first chapter.
So for example $('div') selects all the DOM objects that correspond to div elements; $('#mydiv'") selects the one element with id set to mydiv; $(".bigdiv") selects all elements with class set to bigdiv.
For many jQuery users these are the only selectors needed but if these are the only selectors you know the more general and powerful patterns you can use will not be obvious to you.
In an ideal world you would be able to find every element you needed using just these three selectors but when you have to work with web pages generated by CMS or templates then often you have to work with what you are given. In other words it is sometimes not possible to modify the HTML so that the elements you want to select all have ids or belong to a single class.
It is worth remembering that jQuery always returns an array of elements that match the selector even if there is only one match.
It is also worth remembering that many jQuery methods operate on the first element of the array - which makes working with a single element much easier - or on all of the elements.
Once you graduate beyond the three basic selectors you need to have a framework to organize things - without a framework everything looks like a special case.
The first thing to say is that the fundamental selector is the type selector i.e name which selects all elements corresponding to <name>. All other selectors take the form of extra conditions on this basic selector.
selects elements corresponding to all <p> tags. All other selectors are modifications and extensions on this basic selector.
There is also a universal type selector - the asterisk * which matches every element type. This may not seem to be very useful at first, but it is often used within a larger selector to mean any element type that satisfies other conditions.
This will become clear in a moment.
The basic type selector can be modified with additional conditions - usually indicated by a colon or a square brackets or another special symbol. For example if you want to specify a type with a particular id you can write
to select all <p> elements with id equal to name.
Of course there can only be one element with id equal to name so you don't really need to specify the type. This can be done using the universal type selector i.e.
selects any type of element with id equal to name. The convention is that if you don't specify a type then the universal type selector is assumed. This means that you can simply write
and now you can see that the id selector is just a special case of the more general type selector.
In the same way the .class selector is a short hand for
which selects all elements with the class attribute equal to name. In general the class selector is:
which selects all elements of type T with class set to name.