Recursive walk through DOM elements strange behavior

I'm trying to make a function that walks recursively through the elements bellow:

<body>
    <main class="main">
        <header class="e200-header">
            <a class="umburg side-item" href="#">
                <div>
                    <span class="line line-1"></span>
                    <span class="line line-2"></span>
                    <span class="line line-3"></span>
                </div>
            </a>
        </header>
        <section class="presentation">
        </section>
    </main>
</body>

Open in new window


This is the code that makes it:

function recursiveResolve(node) {
            if (typeof node != 'undefined') {
                //node.someMethod = aFunction

                var l = node.childElementCount

                if (l) {
                    childrens = node.children
    
                    for (var i = 0; i < l; i++) {
                        recursiveResolve(childrens[i])
                    }
                }
            }
        }

recursiveResolve(document.body)

Open in new window


The problem is that, its not working as expected, it is only resolving until the last <span class="line line-3"></span> and stops there when  <section class="presentation"></section> still remaining to be resolved. What i'm doing wrong?
Eleandro DuzentosWeb designer and software developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
It's a problem with your variable scope. On line 8 you're setting a variable called childrens, but you're setting it as a global variable. Change the scope to a local variable and it'll work fine. Declare the variable with the var keyword:

var childrens = node.children;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eleandro DuzentosWeb designer and software developerAuthor Commented:
Thats a little scope issue but because i'm learning it without follow any tutorial it becomes compplicated for me!

You saved my day!!! Thank you so  much!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DOM

From novice to tech pro — start learning today.