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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

36<br />

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

.end()<br />

At times in <strong>jQuery</strong> scripts, you will find it necessary to back up to the last set of elements stored in the<br />

<strong>jQuery</strong> object. The .end() method does exactly that: it reverts the <strong>jQuery</strong> object to the state immediately<br />

preceding the last filtering action in the current <strong>jQuery</strong> object chain.<br />

To select all paragraphs, then find all spans, the original set of paragraphs is no longer available:<br />

>>> $("p").find("span");<br />

[ span, span.foo ]<br />

To revert back to the set of paragraphs, add .end() to the chain:<br />

>>> $("p").find("span").end();<br />

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

Creating <strong>and</strong> Inserting DOM Elements<br />

The first thing you’ll learn that actually changes the DOM, rather than simply selecting elements from it,<br />

is how to create new elements <strong>and</strong> insert them into the DOM. Since the release of <strong>jQuery</strong> 1.4, this is<br />

pretty straightforward.<br />

This section of the book starts using more involved code snippets, <strong>and</strong> will therefore require a minor<br />

adjustment to your Firebug console. At the bottom right of the console, there is a round button with an<br />

arrow pointing upward (see Figure 2-1).

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

Saved successfully!

Ooh no, something went wrong!