Online Book Reader

Home Category

JQuery_ Novice to Ninja - Earle Castledine [12]

By Root 1116 0
and applications appear to load much faster to the end user:

chapter_02/01_document_ready/script.js

$(document).ready(function() {

alert('Welcome to StarTrackr! Now no longer under police …');

});


The important bits here (highlighted in bold) say, “When our document is ready, run our function.” This is one of the most common snippets of jQuery you’re likely to see. It’s usually a good idea to do a simple alert test like this to ensure you’ve properly included the jQuery library—and that nothing funny is going on.

Important: You’ll Be Seeing $(document).ready() a Lot!

Almost everything you do in jQuery will need to be done after the document is ready—so we’ll be using this action a lot. It will be referred to as the document-ready event from now on. Every example that follows in this book, unless otherwise stated, needs to be run from inside the document-ready event. You should only need to declare it once per page though.

The document-ready idiom is so common, in fact, that there’s a shortcut version of it:

$(function() { alert('Ready to do your bidding!'); });


If you’d like to use the shortcut method, go ahead! The expanded version is arguably a better example of self-documenting code; it’s much easier to see at a glance exactly what’s going on—especially if it’s buried in a page of another developer’s JavaScript!

At a cursory glance, the document-ready event looks much removed from the structure we encountered back in our jQuery anatomy class, but on closer inspection we can see that the requisite parts are all accounted for: the selector is document; the action is ready; and the parameter is a function that runs some code (our alert).

Selecting: The Core of jQuery

Time is ticking, and deadlines wait for no one. The client has noted that people have been having quoting incorrect celebrity IDs from the web site. This is because the celebrities’ names are all laid out in one big table and it’s difficult for users to line up a celebrity with the correct reference ID. Our client tells us that he wants every other row to be a light gray color so the users can easily find their favorite celebrity.

We have jQuery ready to do our bidding—it just needs us to choose a target for it. Selecting the elements you want to modify on the page is really the art of jQuery. One of the biggest differences between being a novice and ninja is the amount of time it takes you to grab the elements you want to play with!

You might remember from our jQuery anatomy class that all of our selectors are wrapped in the jQuery function:

jQuery()


Or the alias:

$()


We’ll be using the shortcut alias for the remainder of the book—it’s much more convenient. As we mentioned earlier, there’s no real reason to use the full jQuery name unless you’re having conflict issues with other libraries (see the section called “Avoiding Conflicts” in Chapter 9).

Simple Selecting

Our task is to select alternate table rows on the celebrity table. How do we do this? When selecting with jQuery, your goal should be to be only as specific as required: you want to find out the most concise selector that returns exactly what you want to change. Let’s start by taking a look at the markup of the Celebrities table, shown in Figure 2.1.

Figure 2.1. class and id attributes in the HTML page

We could start by selecting every table row element on the entire page. To select by element type, you simply pass the element’s HTML name as a string parameter to the $ function. To select all table row elements (which are marked up with the tag), you would simply write:

$('tr')


Important: Nothing Happens!

If you run this command, nothing will happen on the page. This is expected—after all, we’re just selecting elements. But there’s no need to worry; soon enough we’ll be modifying our selections in all sorts of weird and wonderful ways.

Similarly, if we wanted to select every paragraph, div element, h1 heading, or input box on the page, we would use these selectors accordingly:

$('p')

$('div')

$('h1')

$('input')

Return Main Page Previous Page Next Page

®Online Book Reader