02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ p, p.foo, p ]<br />

.parent()<br />

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

The .parent() method returns a set of the immediate parent elements of the current selection. For<br />

instance, to select all parent elements of any elements with the class foo, use the following:<br />

$(".foo").parent();<br />

This returns the following:<br />

>>> $(".foo").parent();<br />

[ body, p#bar ]<br />

To match only paragraph elements that are parents of elements with class foo, modify the code to<br />

the following:<br />

$(".foo").parent("p");<br />

This narrows the result set:<br />

>>> $(".foo").parent("p");<br />

[ p#bar ]<br />

.parents() <strong>and</strong> .parentsUntil()<br />

Unlike .parent(), .parents() will return all parent elements, with an optional selector passed to filter<br />

the results.<br />

To select all parent elements of the check box in the form on the example page, use the following<br />

code:<br />

$(":checkbox").parents();<br />

This finds every parent element, all the way out to the html element:<br />

>>> $(":checkbox").parents();<br />

[ label, fieldset, form #, body, html ]<br />

To filter the results so that only the parent form element is returned, modify the code as follows:<br />

$(":checkbox").parents("form");<br />

This returns only the parent form element:<br />

>>> $(":checkbox").parents("form");<br />

33

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!