jQuery Cookbook - Cdn.oreilly.com - O'Reilly
jQuery Cookbook - Cdn.oreilly.com - O'Reilly
jQuery Cookbook - Cdn.oreilly.com - O'Reilly
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
"4 inputs"<br />
alert('selected' + <strong>jQuery</strong>('input',document.forms[0]).length + ' inputs');<br />
//search within the body element for all input elements using an expression,<br />
//alerts "12 inputs"<br />
alert('selected' + <strong>jQuery</strong>('input','body').length + ' inputs');<br />
<br />
<br />
<br />
Discussion<br />
It’s also possible, as mentioned in the solution of this recipe, to select documents as<br />
the context for searching. For example, it’s possible to search within the context of an<br />
XML document that is sent back from doing an XHR request (Ajax). You can find more<br />
details about this usage in Chapter 16.<br />
1.5 Filtering a Wrapper Set of DOM Elements<br />
Problem<br />
You have a set of selected DOM elements in a <strong>jQuery</strong> wrapper set but want to remove<br />
DOM elements from the set that do not match a new specified expression(s) in order<br />
to create a new set of elements to operate on.<br />
Solution<br />
The <strong>jQuery</strong> filter method, used on a <strong>jQuery</strong> wrapper set of DOM elements, can exclude<br />
elements that do not match a specified expression(s). In short, the filter() method<br />
allows you to filter the current set of elements. This is an important distinction from<br />
the <strong>jQuery</strong> find method, which will reduce a wrapped set of DOM elements by finding<br />
(via a new selector expression) new elements, including child elements of the current<br />
wrapped set.<br />
To understand the filter method, let’s examine the following code:<br />
<br />
<br />
<br />
<br />
<br />
<br />
link<br />
link<br />
<br />
link<br />
link<br />
<br />
16 | Chapter 1: <strong>jQuery</strong> Basics