Availability: |
| |||||||
Property/method value type: | Node object | |||||||
JavaScript syntax: | - | myCollection.item(anIndex) | ||||||
- | myCollection.item(aSelector) | |||||||
- | myCollection.item(aSelector, anIndex) | |||||||
- | myCollection[anIndex] | |||||||
Argument list: | anIndex | A zero based index into the collection | ||||||
aSelector | A textual value that selects all matching objects |
This is a search method that traverses a collection looking for an item or collection of items by the index in the collection.
If the first argument is a numeric value, the object at the indexed position is returned. You may not place a second argument in the call. This is the DOM standard specified behavior.
If the first argument is a string, then any object in the collection that has an id or name property that matches the selector will be assembled into another collection. If there is no second argument, that new collection will be returned as a sub-set of the original receiving collection. This is an extension to the DOM specified behavior.
If the first argument is a string and the second argument is a numeric value, the sub-set collection is manufactured but the element in that collection indexed by the second argument is returned as a single object. This is also an extension to the DOM specified behavior.
This extension is useful because you can apply a filter and selection in one call without needing to extract and then store a sub-set collection. On the downside, this will repeat the sub-setting search each time it is called which can lead to performance problems.
When using the myCollection.item(anIndex) syntax variation, it is functionally equivalent to myCollection[anIndex].
Note that the DOM specification does not allow for the alternative array-like addressing mode which is implemented in browsers as a convenience.
You may get back a single object if there is only one item that matches. However, if the selection criteria match more than one item, you will get back an array of objects. This is slightly problematic; it would be better if you consistently got back an array even if it contained zero or only one item. You could then operate on it consistently.
The Item() method of an MSIE Collection object is not the same as the item() method for the DOM NodeList object. The DOM specifies the method name in lower case, though it is upper case in MSIE (although JScript is somewhat forgiving of upper-lower case errors in scripts).
In addition, the Item() method of an MSIE Collection supports several different addressing modes, whereas the item() method of a Nodelist supports only one.
Prev | Home | Next |
Collection object | Up | Collection.length |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |