hide - Understanding jQuery
hide - Understanding jQuery
hide - Understanding jQuery
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
More About CSS Selectors<br />
Selecting things is at the very core of <strong>jQuery</strong> programming. Even though the previous chapter<br />
has already covered <strong>jQuery</strong> CSS Selectors there is much more to selecting HTML elements.<br />
Selecting Things: A Bird’s Eye View<br />
Selecting things doesn’t have to be complicated. However, there are many cases when the<br />
HTML is just way complicated. In many instances, people rush to assign many IDs or CLASSes<br />
to everything and go from there.<br />
The ID and CLASS attributes are not the best tools in this situation. In order to swiftly navigate<br />
the DOM tree structure, <strong>jQuery</strong> presents us with very neat tools. This tutorial will outline<br />
some of the most effective selectors to use in many common situations.<br />
At first, I started this tutorial with the focus only on input elements such as input, textarea,<br />
checkbox, radio button and select/option tags. But eventually as I continued updating it, it<br />
evolved into something much more.<br />
I still cover input elements at the bottom of his page, but I’d like to also talk about many other<br />
ways in which you should be selecting your elements. The best idea is to go through them one<br />
by one, and follow these clear diagrams below, that I think really make a good point of what<br />
each type of a selector will do.<br />
Once you memorize these ways, you will never want to go back to your old ID and CLASS attributes.<br />
One of the reasons for this is that each element already has a name - it’s tag name.<br />
<strong>jQuery</strong> allows us to refer to these tags by their name, and in some cases we can refer to them<br />
by not even using the name or ID of an element we want to select. How is this possible? Let’s<br />
compare the most common mentality of a <strong>jQuery</strong> enthusiast to one of a more experienced<br />
programmer.<br />
The main difference here is that when just starting out people tend to assign ID and CLASS attributes<br />
to everything. Is this the right way of doing things?<br />
Figure 1. explains mentality of the beginner <strong>jQuery</strong> programmer because everything is assigned<br />
an ID (and in many other cases a class, though, this is not shown on the diagram).<br />
Why is this a popular mentality of someone who just started out using <strong>jQuery</strong>? Well, it’s probably<br />
because they only learned how to select elements by ID or a Class name and refused to<br />
learn other types of selections one can make using advanced selectors and DOM traversal<br />
techniques that <strong>jQuery</strong> has in its arsenal.<br />
86