1

The value of elParentAttr is 'level4' the first time traverseUp is called but every time after is null. Why is this? Fiddle here.

JavaScript:

function traverseUp(el, attr) {
    var elParent = el.parentNode,
        elParentAttr = el.parentNode.getAttribute(attr);

    console.log(elParent);
    console.log(elParentAttr);

    traverseUp(elParent);
}

traverseUp((document.getElementsByClassName('level5'))[0], 'class');

Document:

<div class="level1">
    <div class="level2">
        <div class="level3">
            <div class="level4">
                <div class="level5"></div>
            </div>
        </div>
    </div>
</div>

1 Answer 1

4

When you're calling it with the loop, aren't you missing the second parameter?

from traverseUp(elParent); to traverseUp(elParent, attr); ?

Sign up to request clarification or add additional context in comments.

1 Comment

@maxhallinan No problem, don't forget the green tick :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.