Packageelementtraversal
Interfacepublic interface ElementTraversal
Implementors Element

Allows script navigation of the elements of a DOM tree, excluding all other nodes in the DOM, such as text nodes. It also provides an attribute to expose the number of child elements of an element. It is intended to provide a more convenient alternative to existing DOM navigation interfaces, with a low implementation footprint.

All browser compatibility information was obtained via Quirksmode.

See also

MDC - Element
W3C Element Traversal Specification
Quirksmode: W3C DOM Compatibility - Traversal


Public Properties
 PropertyDefined By
  Element Traversal childElementCount : Number
[read-only] Returns the current number of element nodes that are children of this element.
ElementTraversal
  Element Traversal firstElementChild : Element
[read-only] Returns the first child element node of this element.
ElementTraversal
  Element Traversal lastElementChild : Element
[read-only] Returns the last child element node of this element.
ElementTraversal
  Element Traversal nextElementSibling : Element
[read-only] Returns the next sibling element node of this element.
ElementTraversal
  Element Traversal previousElementSibling : Element
[read-only] Returns the previous sibling element node of this element.
ElementTraversal
Property Detail
Element Traversal childElementCountproperty
childElementCount:Number  [read-only]

Product Versions : Internet Explorer 5.5 Internet Explorer 6.0 Internet Explorer 7.0 Internet Explorer 8.0 as IE7 Internet Explorer 8.0 as IE8 Firefox 2.0 Firefox 3.0 Firefox 3.5 Safari 3.0 Safari 3.1 Safari 4.0 Chrome 1.0 Chrome 2.0 Opera 9.62 Opera 10.0b

Returns the current number of element nodes that are children of this element. 0 if this element has no child nodes that are of nodeType 1.


Implementation
    public function get childElementCount():Number

See also

Element Traversal firstElementChildproperty 
firstElementChild:Element  [read-only]

Product Versions : Internet Explorer 5.5 Internet Explorer 6.0 Internet Explorer 7.0 Internet Explorer 8.0 as IE7 Internet Explorer 8.0 as IE8 Firefox 2.0 Firefox 3.0 Firefox 3.5 Safari 3.0 Safari 3.1 Safari 4.0 Chrome 1.0 Chrome 2.0 Opera 9.62 Opera 10.0b

Returns the first child element node of this element. null if this element has no child elements.


Implementation
    public function get firstElementChild():Element

See also


Example
In this example, the alert shows "SPAN", which is the name of the first child node of the paragraph element.
         <p id="para-01">
             <span>First span</span>
         </p>
         
         <script type="text/javascript">
             var p01 = document.getElementById('para-01');
             alert(p01.firstElementChild.nodeName);
         </script>
Element Traversal lastElementChildproperty 
lastElementChild:Element  [read-only]

Product Versions : Internet Explorer 5.5 Internet Explorer 6.0 Internet Explorer 7.0 Internet Explorer 8.0 as IE7 Internet Explorer 8.0 as IE8 Firefox 2.0 Firefox 3.0 Firefox 3.5 Safari 3.0 Safari 3.1 Safari 4.0 Chrome 1.0 Chrome 2.0 Opera 9.62 Opera 10.0b

Returns the last child element node of this element. null if this element has no child elements.


Implementation
    public function get lastElementChild():Element

See also


Example
In this example, the alert shows "B", which is the name of the last child node of the paragraph element.
         <p id="para-01">
             <span>First span</span>
             <b>bold</b>
         </p>
         
         <script type="text/javascript">
             var p01 = document.getElementById('para-01');
             alert(p01.lastElementChild.nodeName);
         </script>
Element Traversal nextElementSiblingproperty 
nextElementSibling:Element  [read-only]

Product Versions : Internet Explorer 5.5 Internet Explorer 6.0 Internet Explorer 7.0 Internet Explorer 8.0 as IE7 Internet Explorer 8.0 as IE8 Firefox 2.0 Firefox 3.0 Firefox 3.5 Safari 3.0 Safari 3.1 Safari 4.0 Chrome 1.0 Chrome 2.0 Opera 9.62 Opera 10.0b

Returns the next sibling element node of this element. null if this element has no element sibling nodes that come after this one in the document tree.


Implementation
    public function get nextElementSibling():Element

See also


Example
         <div id="div-01">Here is div-01</div>
         <div id="div-02">Here is div-02</div>
         
         <script type="text/javascript">
             var el = document.getElementById('div-01').nextElementSibling;
             document.write('<p>Siblings of div-01</p><ol>');
             while (el) {
                 document.write('<li>' + el.nodeName + '</li>');
                 el = el.nextElementSibling;
             }
             document.write('</ol>');
         </script>

This example outputs the following into the page when it loads:

Siblings of div-01

  1. DIV
  2. SCRIPT
  3. P
  4. OL
Element Traversal previousElementSiblingproperty 
previousElementSibling:Element  [read-only]

Product Versions : Internet Explorer 5.5 Internet Explorer 6.0 Internet Explorer 7.0 Internet Explorer 8.0 as IE7 Internet Explorer 8.0 as IE8 Firefox 2.0 Firefox 3.0 Firefox 3.5 Safari 3.0 Safari 3.1 Safari 4.0 Chrome 1.0 Chrome 2.0 Opera 9.62 Opera 10.0b

Returns the previous sibling element node of this element. null if this element has no element sibling nodes that come before this one in the document tree.


Implementation
    public function get previousElementSibling():Element

See also


Example
         <div id="div-01">Here is div-01</div>
         <div id="div-02">Here is div-02</div>
         <li>This is a list item</li>
         <li>This is another list item</li>
         <div id="div-03">Here is div-03</div>
         
         <script type="text/javascript">
             var el = document.getElementById('div-03').previousElementSibling;
             document.write('<p>Siblings of div-03</p><ol>');
             while (el) {
                 document.write('<li>' + el.nodeName + '</li>');
                 el = el.previousElementSibling;
             }
             document.write('</ol>');
         </script>

This example outputs the following into the page when it loads:

Siblings of div-03

  1. LI
  2. LI
  3. DIV
  4. DIV