About this Book

Do more with less.

Stated plainly and simply, that is the purpose of this book: to help you learn how to do more on your web application pages with less script. Your authors, one an avid and enthusiastic user, and the other a jQuery contributor and evangelist, believe that jQuery is the best library available today to help you do just that.

This book is aimed at getting you up and running with jQuery quickly and effectively while having some fun along the way. All the APIs for the core jQuery library and its companion jQuery UI library are discussed, and each API method is presented in an easy-to-digest syntax block that describes the parameters and return values of the method. Many examples of effective use of the APIs are included; and, for those big concepts, we provide what we call Lab pages. These comprehensive and fun pages are an excellent way for you to see the nuances of the jQuery methods in action, without the need to write a slew of code yourself.

All example code and Lab pages are available for download at http://www.manning. com/jQueryinActionSecondEdition or http://www.manning.com/bibeault2.

We could go on and on with some marketing jargon telling you how great this book is, but you don’t want to waste time reading that, do you? What you really want is to get into the bits and bytes up to your elbows, isn’t it? Well, that’s exactly the intention of this book.

What’s holding you back? Read on!

Audience

This book is aimed at web developers, from novice to advanced, who want to take control of the JavaScript on their pages and produce great, interactive web applications without the need to write all the raw, browser-dependent client-side code necessary to achieve such applications from scratch.

All web developers who wish to leverage the power of jQuery to create highly interactive and usable web applications that delight rather than annoy their users will benefit from this book.

Although novice web developers may find some sections a tad involved, this should not deter them from diving into this book. We’ve included an appendix on essential JavaScript concepts that will help in using jQuery to its fullest potential, and such readers will find that the jQuery library itself is novice-friendly once they understand a few key concepts. And even though jQuery is friendly to novices, it makes plenty of power available to more advanced web developers.

Whether novices or veterans of web development, client-side programmers will benefit greatly from adding jQuery to their repertoire of development tools. We know that the lessons within this book will help add this knowledge to your toolbox quickly.

Roadmap

This book is organized to help you wrap your head around jQuery and jQuery UI in the quickest and most efficient manner possible. It starts with an introduction to the design philosophies on which jQuery was founded and quickly progresses to fundamental concepts that govern the jQuery API. We then take you through the various areas in which jQuery can help you write exemplary client-side code, from the handling of events all the way to making Ajax requests to the server. To top it all off, we take an extensive tour through the jQuery UI companion library.

The book is divided into two parts: the first covering the core jQuery library, and the second dealing with jQuery UI. Part 1 consists of eight chapters.

In chapter 1, we’ll learn about the philosophy behind jQuery and how it adheres to modern principles such as Unobtrusive JavaScript. We examine why we might want to adopt jQuery, and we run through an overview of how it works. We delve into key concepts such as document-ready handlers, utility functions, Document Object Model (DOM) element creation, and how jQuery extensions are created.

Chapter 2 introduces us to the jQuery wrapped set—the core concept around which jQuery operates. We’ll learn how this wrapped set-a collection of DOM elements that’s to be operated upon—can be created by selecting elements from the page document using the rich and powerful collection of jQuery selectors. We’ll see that these selectors use standard CSS notation, which makes them quite powerful even as they leverage the CSS knowledge we most likely already possess.

In chapter 3, we’ll learn how to use the jQuery wrapped set to manipulate the page DOM. We’ll cover changing the styling and attributes of elements, setting element content, moving elements around, creating elements from scratch, and dealing with form elements.

Chapter 4 shows us how we can use jQuery to vastly simplify the handling of events on our pages. After all, handling user events is what makes interactive web applications possible, and anyone who’s had to deal with the intricacies of event handlers across the differing browser implementations will certainly appreciate the simplicity that jQuery brings to this particular area. Advanced event-handling concepts, such as event namespacing, custom event triggering and handling, and even establishing of proactive “live” handlers, are examined in detail and brought together in a comprehensive example project.

The world of animations and effects is the subject of chapter 5. We’ll see how jQuery makes creating animated effects not only painless but also efficient and fun. Function queuing, for serially running effects as well as general functions, is covered in detail.

In chapter 6, we’ll learn about the utility functions and flags that jQuery provides, not only for page authors, but also for those who will write extensions and plugins for jQuery.

We’ll focus on writing such extensions and plugins in chapter 7. We’ll see how jQuery makes it extraordinarily easy for anyone to write such extensions without intricate JavaScript or jQuery knowledge and why it makes sense to write any reusable code as a jQuery extension.

Chapter 8 concerns itself with one of the most important areas in the development of modern web applications: making Ajax requests. We’ll see how jQuery makes it almost brain-dead simple to use Ajax on our pages, shielding us from all the usual pitfalls, while vastly simplifying the most common types of Ajax interactions (such as returning JSON constructs). Another comprehensive example project brings all that we’ve learned about jQuery into focus.

In part 2, which consists of three chapters, we’ll explore jQuery’s essential companion library: jQuery UI.

Chapter 9 introduces jQuery UI, and explains how to configure and obtain a customized version of the library code, as well as the visual themes used to style the elements of the UI library. The visual themes are dissected so that we can learn not only how they’re constructed, but also how we can modify them to our own needs. Rounding out the chapter is a discussion of the extended effects that jQuery UI adds to the jQuery core, as well as how core methods are augmented to take advantage of those extensions.

In chapter 10, we explore the mouse interaction capabilities that jQuery UI provides. This extends from dragging and dropping to handling the sorting, selection, and resizing of elements.

Finally, chapter 11 wraps up with a thorough examination of the widget set provided by jQuery UI to extend the available set of input mechanisms we can present on our pages. This includes such simple controls as buttons through to more sophisticated controls such as date pickers, autocompleters, tabbed panes, and dialog boxes.

To top it all off, we have provided an appendix highlighting key JavaScript concepts such as function contexts and closures—essential to making the most effective use of jQuery on our pages—for readers who are unfamiliar with, or who would like a refresher on, these concepts.

Margin icons

Throughout this book, unique Lab pages are introduced to help illustrate jQuery and jQuery UI concepts. These labs pages are interactive web pages, provided with the downloaded example code, that you can run on your own local system.

Lab iconWhen a new Lab page is introduced, a Labs Icon (the flask icon show at left) is placed in the left margin. This makes it easy for you to find the location within the text where a lab is first described. A handy list of the Lab pages is also included right after the Table of Contents, and links to the labs are provided in the index page of the downloaded example code.

You can also access the Labs, as well as the rest of the example code, remotely by pointing your browser at http://www.bibeault.org/jqia2 or from the publisher’s website at http://www.manning.com/jQueryinActionSecondEdition.

Exercise iconAnother margin icon you will find sprinkled throughout the book is the Exercises Icon (the triangle and pencil), which pinpoints text passages where exercises that you should work through are presented. Frequently, these exercises will be related to a particular Lab page, but sometimes they are logical extensions of other code examples described throughout the book, or simply standalone exercises that you should solve to make sure that the concepts are gelling in your mind.

All source code in listings or in the text is in a >code>fixed-width font like this to separate it from ordinary text. Method and function names, properties, XML elements, and attributes in the text are also presented in this same font.

In some cases, the original source code has been reformatted to fit on the pages. In general, the original code was written with page-width limitations in mind, but sometimes you may find a slight formatting difference between the code in the book and that provided in the source download. In a few rare cases, where long lines could not be reformatted without changing their meaning, the book listings contain line-continuation markers.

Code annotations accompany many of the listings, highlighting important concepts. In many cases, numbered bullets link to explanations that follow in the text.

Code downloads

Source code for all the working examples in this book (along with some extras that never made it into the text) is available for download from the book’s web page at http://www.manning.com/jQueryinActionSecondEdition. For the convenience of those who may not be able to run the examples locally for whatever reason, a working version of the examples is available online at http://www.bibeault.org/jqia2/.

The code examples for this book are organized into a web application, with separate sections for each chapter, ready to be easily served by a local web server such as the Apache HTTP Server. You can simply unzip the downloaded code into a folder of your choice, and make that folder the document root of the application. A launch page is set up at the application root in the index.html file.

With the exception of the examples for the Ajax chapter (chapter 8) and a handful from the jQuery UI chapters, most of the examples don’t require the presence of a web server at all and can be loaded directly into a browser for execution, if you so desire. The Ajax examples require more backend interaction than Apache can deliver, so running them locally requires either setting up PHP for Apache, or running a web server capable of executing Java servlets and JavaServer Pages (JSP), such as Tomcat. Instructions for easily setting up Tomcat to use as the web server for the Ajax examples are provided in the chapter8/tomcat.pdf file.

All examples were tested in a variety of browsers that include Internet Explorer 7 and 8, Firefox 3, Safari 3 and 4, and Google Chrome.

Author Online

The purchase of jQuery in Action, Second Edition includes free accessto a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and other users. To access and subscribe to the forum, point your browser to http://www.manning. com/jQueryinActionSecondEdition, and click the Author Online link. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct in the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It’s not a commitment to any specific amount of participation on the part of the authors, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.