Convert NodeList to an Array

 
<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 
    </head> 

    
    <body> 
        <div id="results">
           <code><code>
           <p></p>
        </div> 
        <script> 
            var resultsElement = document.getElementById("results"); 
            document.writeln(resultsElement.nodeType); 
            document.writeln(resultsElement.nodeName); 
            
            //won't work in IE8 and earlier 
            var arrayOfNodes = Array.prototype.slice.call(resultsElement.childNodes,0); 
            document.writeln(arrayOfNodes.length); 
        </script>  
    </body> 
</html>
  
Click to view the demo

To convert a NodeList to an array in Internet Explorer, you must manually iterate over the members.

 
<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Example</title> 
    </head> 

    
    <body> 
        <div id="results">
           <code><code>
           <p></p>
        </div> 
        <script> 
            function convertToArray(nodes){ 
                var array = null; 
                try {
                     array = Array.prototype.slice.call(nodes, 0); //non-IE and IE9+
                } catch (ex) { 
                    array = new Array(); 
                    for (var i=0, len=nodes.length; i < len; i++){
                        array.push(nodes[i]); 
                    } 
                }
                return array; 
            } 

        
            var resultsElement = document.getElementById("results"); 
            document.writeln(resultsElement.nodeType); 
            document.writeln(resultsElement.nodeName); 
            
            var arrayOfNodes = convertToArray(resultsElement.childNodes); 
            document.writeln(arrayOfNodes.length); 
        </script>  
    </body> 
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    DOM  

DOM Model:
  1. Document Object Model
  2. nodeName and nodeValue Properties
  3. Node Relationships:childNodes
  4. parentNode
  5. previousSibling and nextSibling properties
  6. lastChild and firstChild
  7. appendChild() adds a node to the end of the childNodes list
  8. insertBefore()
  9. ownerDocument property
  10. removeChild
  11. replaceChild()
  12. Working with Text
  13. Check the length of NodeList
  14. Convert NodeList to an Array
  15. html tag and its cooresponding JavaScript class