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.

40<br />

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

Changes made with JavaScript can be saved on the server through the use of AJAX (which you’ll<br />

learn about later in this chapter), which allows JavaScript to interface with server-side languages such as<br />

<strong>PHP</strong>.<br />

■ Note After performing the examples in each of the following sections, refresh your page so each new example<br />

is starting with a fresh copy of the example HTML file.<br />

.append() <strong>and</strong> .prepend()<br />

The .append() <strong>and</strong> .prepend() functions will attach the elements passed as arguments to the <strong>jQuery</strong><br />

object to which they are chained. The only difference is that .append() attaches the elements at the end,<br />

<strong>and</strong> .prepend() attaches at the beginning.<br />

The content will be appended or prepended inside the matched elements, meaning if you match all<br />

paragraphs on the page <strong>and</strong> append a new sentence, “This was added <strong>by</strong> <strong>jQuery</strong>”, it will be appended<br />

inside the closing paragraph tag ().<br />

Try this out <strong>by</strong> entering the following code into your console:<br />

$("p").append(" This was added <strong>by</strong> <strong>jQuery</strong>.");<br />

Executing the code will add this sentence to the end of each paragraph inside the closing paragraph<br />

tag. This is evident because the text is not knocked to the next line, as it would be if it were outside the<br />

closing tag.

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

Saved successfully!

Ooh no, something went wrong!