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.foo").next();<br />

This generates the following output:<br />

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

[ p ]<br />

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

A selector can be passed to .next() as well, which allows developers to determine which type of next<br />

sibling element should be matched:<br />

$("p.foo").next("#bar");<br />

This returns an empty result set, since the next element does not have an ID of bar:<br />

>>> $("p.foo").next("#bar");<br />

[ ]<br />

Because .next() returns only one element, a companion method was created that returns all next<br />

sibling elements, .nextAll(). To select all paragraphs after the paragraph with the class foo, use the<br />

following code:<br />

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

This returns the following result:<br />

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

[ p, p#bar ]<br />

■ Note The selector is optional in .nextAll(), as it is in .next().<br />

The third method available for selecting next sibling elements is the .nextUntil() method. As its<br />

name suggests, this method will return all next elements until a selector is matched. It’s important to<br />

note that the element matched <strong>by</strong> the selector will not be included in the result set.<br />

To demonstrate this, select the paragraph with the class foo <strong>and</strong> use .nextUntil() with a selector of<br />

"#bar":<br />

$(".foo").nextUntil("#bar");<br />

Only one paragraph is returned in the result set, <strong>and</strong> the paragraph with the ID of bar is not<br />

included:<br />

>>> $(".foo").nextUntil("#bar");<br />

31

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

Saved successfully!

Ooh no, something went wrong!