Here’s a quick example: say you have a table of data to which you wish to apply a striping-effect to alternate rows. All odd rows you wish to display with a pale yellow background, all even rows with a pale blue background.
The traditional, coding-by-hand HTML method you’d manually have to add a class to each table row. If you were then to add a new row in the middle of the table you’d then have to again manually re-edit the code in each and every row.
jQuery offers the following solution:
- Download the jQuery library (about 14 KB compressed) and link it to your HTML document
- Create new classes in your CSS file for styling the .odd and .even rows.
Here’s the jQuery code you’d need:
Four lines of code is all it takes, and it’s almost self-explanatory too.
The code says:
- Wait until the DOM is ready then execute the code
- Then add class=”odd” to all odd table rows
- Add class=”even” to all even table rows
The book is very easy to read, taking everything step-by-step and arranged in a very logical and easy to follow way.
Chapter 1 introduces the reader to jQuery
Chapter 2 covers selectors, the building-blocks of a jQuery function
Chapter 3 looks at events: when to start your jQuery code (e.g. on page load, when you click a link, etc.)
Chapter 4 examines Effects, e.g. fading in and out
Chapter 5 covers DOM manipulation: how to change your page on command
Chapter 6 promises to make your website buzzword-compliant with AJAX
The following chapters cover table manipulation, enhancing forms, shufflers and rotators, and extending the built-in functionality of jQuery with plug-ins.
You can see the book examples in action at http://book.learningjquery.com.
I can’t wait to get started and incorporate some of these techniques into my websites.
(Gareth J M Saunders)