hide - Understanding jQuery
hide - Understanding jQuery
hide - Understanding jQuery
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Let’s say that we have a form residing in an element whose class name is .clockwork. And we<br />
want to select all input elements that happen to be using class .orange within that form.<br />
<br />
<br />
<br />
<br />
<br />
<br />
Using the .orange handle is just another way of referring to an input box. Let’s take a look at<br />
how we would do this without using context first. This selector will select all visible input elements<br />
anywhere in the DOM.<br />
And with context?<br />
$(‘.orange:visible’)<br />
$(‘.orange:visible’, $(‘.clockwork form’))<br />
But this would be a step backward. We just doubled the amount of work for <strong>jQuery</strong> by using<br />
two complex selectors, not just one.<br />
Remember our first rule of Javascript performance? Since we already know that using $(‘#id_<br />
name’) is the absolute fastest way to perform a DOM selection, we will use that knowledge to<br />
our advantage. You should always aim to simplify your selectors in such a way if you’re looking<br />
for a performance advantage.<br />
Instead, let’s rewrite the example from above to make it fast:<br />
$(‘.orange:visible’, $(‘#my_form’))<br />
Now this will cut the performance bottleneck of your <strong>jQuery</strong> selectors. The more expensive<br />
selector .orange:visible now works within the confines of a less expensive selector #my_form.<br />
In fact, this is the fastest selector you can possibly get with <strong>jQuery</strong>.<br />
Remember the general rule when speeding up the performance of your <strong>jQuery</strong> selectors:<br />
Confine a complex selector to a less complex context.<br />
102