Web Development with jQuery®

(Elliott) #1

(^136) ❘ CHAPTER 5 ITERATION OF ARRAYS AND OBJECTS
You have an array of items, a static node list, a live node list, or possibly an object. (By the way, in
JavaScript, all arrays are objects, but not all objects are arrays.) Then you make a for loop, defi ne a
counter, and proceed to iterate over the contents of your array or list. If instead you want to iterate
over an object, you’d instead use a for/in construct to look at the properties of the object.
jQuery makes creating a for construct unnecessary by providing a way to iterate over an array
or list using a function call instead of a for loop and a callback function that’s called for each
individual item present in the array or object. Inside that callback function, you can do something
with each individual item contained in the array, object, or list.
jQuery provides multiple functions for enumeration, which are covered in this chapter. The function
that jQuery provides for basic enumeration is called each(), and it is demonstrated via two possible
ways of application, in the following example, Example 5-1:
<!DOCTYPE HTML>






The Beatles Discography






The Beatles




Discography






The preceding markup document is linked to the following style sheet:
body {
font: 12px "Lucida Grande", Arial, sans-serif;
color: rgb(50, 50, 50);
margin: 0 ;
padding: 0 10px;
}
body ul {
list-style: none;
margin: 0 0 10px 0 ;
padding: 10px;
background: yellow;
width: 250px;
}
[http://www.it-ebooks.info](http://www.it-ebooks.info)
Free download pdf