The elements in the collection are sorted in source code order.
A live collection means that the modification of the document affects the collection.
See Example 2 for details.
Returns a node or a collection of nodes from the current collection by name or index.
Parameters:
nameOrIndex
Required. String or zero-based integer that specifies the node or nodes to retrieve.
If an integer value is specified, it specifies the index of the node to retrieve.
If this parameter is a string, then it specifies a value for the name or id property of the element or elements to retrieve.
Return value:
If no match is found, it returns undefined.
If exactly one match is found, it returns the matching node.
If more than one match is found, it returns a NodeList sub-collection filled with the matching elements.
The sub-collection contains the matching elements in source order.
In Firefox, Opera, Google Chrome and Safari, if more than one match is found, the first matching element is returned.
Returns an node or a collection of nodes from the current collection by name or index.
Parameters:
nameOrIndex
Required. String or zero-based integer that specifies the node or nodes to retrieve.
If an integer value is specified, it specifies the index of the node to retrieve.
If this parameter is a string, then it specifies a value for the name or id property of the element or elements to retrieve.
Firefox, Google Chrome and Safari do not support string values.
subIndex
Optional. Supported by Internet Explorer only.
If more than one matching element is found for the index parameter (possible only if it is a string), the item method creates a NodeList sub-collection filled with the matching elements.
In that case, the subIndex property specifies the position of the element in the sub-collection to retrieve.
In other cases, the subIndex property has no meaning.
Return value:
If no match is found, it returns null in Internet Explorer, Firefox and Opera, and returns undefined in Google Chrome and Safari.
If exactly one match is found, it returns the matching node.
If more than one match is found, it returns a NodeList sub-collection filled with the matching elements.
The sub-collection contains the matching elements in source order.
In Opera, if more than one match is found, the first matching element is returned.
Returns an element or a collection of elements from the current collection by name.
Parameters:
name
Required. String that specifies a value for the name or id property of the element or elements to retrieve.
Return value:
If no match is found, it returns null.
If exactly one match is found, it returns the matching element.
If more than one match is found, it returns a NodeList sub-collection filled with the matching elements.
The sub-collection contains the matching elements in source order.
In Firefox and Opera, if more than one match is found, the first matching element is returned.
Returns a NodeList collection that contains all elements to which the specified behavior is attached.
Example HTML code 1:
This example illustrates the use of the NodeList collection:
<head><scripttype="text/javascript">function GetCheckedFruits () {
var elements = document.getElementsByName ("fruits");
for (var i=0; i < elements.length; i++) {
if (elements[i].checked) {
alert ("The " + (i + 1) + ". radio button is checked");
}
}
}
</script></head><body><buttononclick="GetCheckedFruits ()">Get the checked fruit!</button>
apple <inputtype="radio"name="fruits"checked="checked"/>
peach <inputtype="radio"name="fruits"/>
cherry <inputtype="radio"name="fruits"/></body>
This example illustrates the meaning of live collections:
<head><scripttype="text/javascript">function GetBoldElements () {
var container = document.getElementById ("container");
var boldTags = container.getElementsByTagName ("b");
alert ("There are " + boldTags.length + " bold elements in the container.");
// add a new bold element to the container
var newBoldTag = document.createElement ("b");
newBoldTag.innerHTML = "new bold element";
container.appendChild (newBoldTag);
alert ("A new bold tag has been added to the container.");
alert ("There are " + boldTags.length + " bold elements in the container.");
// remove a bold element from the container
container.removeChild (newBoldTag);
alert ("A bold tag has been removed from the container.");
alert ("There are " + boldTags.length + " bold elements in the container.");
}
</script></head><body><buttononclick="GetBoldElements ()">Get the number of bold elements in the container!</button><br/><br/><divid="container"><b>First bold element in the container.</b><br/><b>Second bold element in the container.</b></div></body>