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.

34<br />

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

[ form # ]<br />

Finally, to select a range of parents until a selector is matched, similar to .nextUntil() or<br />

.prevUntil(), use .parentsUntil():<br />

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

This returns all parent elements until the form element is encountered:<br />

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

[ label, fieldset ]<br />

.add()<br />

The .add() method is versatile <strong>and</strong>, therefore, a bit more complicated. Essentially, it allows you to add<br />

additional elements to the existing <strong>jQuery</strong> object using a selector or a string of HTML.<br />

To select all paragraphs <strong>and</strong> then add the span with class foo to the object, use the following:<br />

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

This outputs the following:<br />

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

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

The .add() method also allows you to create elements on the fly, like so:<br />

$("p").add('This is a new span');<br />

Executing the preceding code will output this:<br />

>>> $("p").add('This is a new span');<br />

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

■ Note Notice that the element span#bat is faded in the console output. This happens because, while the element<br />

exists in the <strong>jQuery</strong> object, it has not been appended to the DOM <strong>and</strong>, therefore, does not display on the page.<br />

You'll learn how to add new elements to the DOM in the next section, “Creating <strong>and</strong> Inserting DOM Elements.”

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

Saved successfully!

Ooh no, something went wrong!