chris-ELM
1/19/2017 - 4:09 PM

Count number of divs within a div. Can count child divs (Boolean)

Count number of divs within a div. Can count child divs (Boolean)

function getCount(parent, getChildrensChildren) {
            var relevantChildren = 0;
            var children = parent.childNodes.length;
            for (var i = 0; i < children; i++) {
               
                if (parent.childNodes[i].nodeName == 'DIV') {
                     console.log(parent.childNodes[i].nodeName)
                    if (getChildrensChildren)
                        relevantChildren += getCount(parent.childNodes[i], true);
                    relevantChildren++;
                }
            }
            return relevantChildren;
        }
        
        alert(getCount(numOfItems, false)); // Get all child node count