12.07.2015 Views

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

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.

CHAPTER 2 GETTING STARTED• Caution A common mistake is to <strong>for</strong>get to wrap the <strong>JavaScript</strong> statements to be executed in a function, whichcauses an odd effect. If you pass a single statement to the ready method, then it will be executed as soon as thebrowser processes the script element. If you pass multiple statements, then the browser will usually report a<strong>JavaScript</strong> error.The ready method creates a handler <strong>for</strong> the ready event. I’ll show you more of the way that jQuerysupports events later in this chapter. The ready event is available only <strong>for</strong> the document object, which iswhy you will see the statements highlighted in the listing in almost every web app that uses jQuery.Selecting and Hiding the Input ElementNow that I have delayed the execution of the <strong>JavaScript</strong> code until the DOM is ready, I can turn to thenext step in my task, which is to hide the input element that submits the <strong>for</strong>m. Listing 2-4 highlights thestatement from the example that does just this.Listing 2-4. Selecting and Hiding the input Element...$(document).ready(function() {$('#buttonDiv input:submit').hide();$('Submit Order').appendTo("#buttonDiv");})...This is a classic two-part jQuery statement: first I select the elements I want to work with, and then Iapply a jQuery method to modify the selected elements. You may not recognize the selector I have usedbecause the :submit part is one of the selectors that jQuery defines in addition to those in the CSSspecification. Table 2-1 contains the most useful jQuery custom selectors.19www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!