Web Development with jQuery®

(Elliott) #1

(^58) ❘ CHAPTER 2 SELECTING AND FILTERING
setUpThisWeek();
}
);
Finally, the last thing that you do within the anonymous function assigned to the ready() event is
to call setUpThisWeek(), which sets up the week relative to the element considered today when the
document is initially loaded.
setUpThisWeek();
The last concepts presented in this chapter are just some notes about two additional methods that
jQuery provides. In my own experience, I haven’t had a cause to use these methods often, but they
may be useful to you. Those two methods are slice() and add().


Slicing a Selection


The slice() method is similar to the eq() method; it selects a subset of a selection based on the off-
set position of elements in a selection. It does this using one or two arguments. If you provide just
one argument, you provide the starting point for the slice. Take the following example of the places
of Middle Earth:

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type"
content="application/xhtml+xml; charset=utf-8" />
<meta http-equiv="content-language" content="en-us" />
<title>Places in Middle-Earth</title>
</head>
<body>
<ul>
<li>The Shire</li>
<li>Fangorn Forest</li>
<li>Rohan</li>
<li>Gondor</li>
<li>Mordor</li>
</ul>
</body>
</html>

Using $('li').slice(1), the argument 1 indicates where the slice begins, so slice(1) would include
all elements from <li>Fangorn Forest</li> to <li>Mordor</li>. Therefore, using a single argument,
counting from zero, your selection includes that element onward.

When supplying two arguments, the fi rst argument is the offset position of the fi rst element you’d
like to include in the resulting selection counting from zero, and the second argument is the offset
position of the last element you’d like to include in the resulting selection, also offset from zero.
This creates a new selection where the elements in that selection include a range of elements start-
ing with that fi rst element and ending with the last element. So, slice(0, 2) would start with
element number zero and end with element number 1. The selection goes from 0–2, but it does

http://www.it-ebooks.info

Free download pdf