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.

44<br />

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

After executing the preceding code in your console, a new paragraph with a yellow background<br />

appears at the top of your browser window (see Figure 2-6).<br />

Figure 2-6. The new paragraph as it appears after prepending it to the body element<br />

.appendTo() <strong>and</strong> .prependTo()<br />

In the last example, you had to create an element, store it, <strong>and</strong> then select the element to which it was<br />

appended. This can be a somewhat roundabout approach, but fortunately, <strong>jQuery</strong> provides .appendTo()<br />

<strong>and</strong> .prependTo(), which chain instead to the object to be appended <strong>and</strong> accept the selector of the<br />

element to which you wish to append.<br />

Using the last example as a starting point, to add the same paragraph element to the body using<br />

.prependTo(), your code would simplify thusly:<br />

$("", {<br />

"text":"I'm a new paragraph!",<br />

"css":{"background":"yellow"}<br />

})<br />

.prependTo("body");

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

Saved successfully!

Ooh no, something went wrong!