NodeList collection
Represents a live collection of nodes.
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.
A live collection means that the modification of the document affects the collection. See Example 2 for details.
Syntax:
Methods that return the object:
+ | object.getElementsByClassName (className) |
+ | object.getElementsByName (name) |
+ | object.getElementsByTagName (tagName) |
+ | object.getElementsByTagNameNS (namespaceURI, tagName) |
+ | object.querySelectorAll (selectors) |
+ | object.tags (tagName) |
+ | object.urns (URN) |
The base interface, through which you can add new functionalities to the NodeList collection, is the NodeList interface.
Possible members:
Properties:
Returns an integer that specifies the number of nodes in the current collection.
This property is read-only. |
Methods:
[nameOrIndex] |
Returns a node or a collection of nodes from the current collection by name or index.
Parameters:
Return value:
|
||||||||||||||||||||||
item (nameOrIndex [, subIndex]) |
Returns an node or a collection of nodes from the current collection by name or index.
Parameters:
Return value:
|
||||||||||||||||||||||
namedItem (name) |
Returns an element or a collection of elements from the current collection by name.
Parameters:
Return value:
|
||||||||||||||||||||||
tags | Returns a NodeList collection that contains all elements from the current collection with the specified tag name. | ||||||||||||||||||||||
urns | 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> <script type="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> <button onclick="GetCheckedFruits ()">Get the checked fruit!</button> apple <input type="radio" name="fruits" checked="checked" /> peach <input type="radio" name="fruits" /> cherry <input type="radio" name="fruits" /> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the meaning of live collections:
|
||||
<head> <script type="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> <button onclick="GetBoldElements ()">Get the number of bold elements in the container!</button> <br /><br /> <div id="container"> <b>First bold element in the container.</b> <br /> <b>Second bold element in the container.</b> </div> </body> |
||||
|
||||
Did you find this example helpful?
|
External links:
User Contributed Comments