Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Unable to iterate over DOM's childElementCount result #13

Description

@mmarquardt610

We use the inline code suite in the learning platform for Bloc, and we're running into an issue trying to teach DOM manipulation in javascript. We define a simple DOM in the HTML tab, and attempting to iterate over the list items in an unordered list is throwing a TypeError.

JS tab:

var x = document.getElementById("list").childElementCount;
console.log(x, typeof x);
var num = 4;
console.log(num, typeof num);
for (i = 0; i < x; i++) {
     console.log(i);
}

HTML tab:

<h3>Shirts</h3>
<ul id='list'>
    <li>Biker Jacket</li>
    <li>Mens Shirt</li>
</ul>

When running, the console prints this error:

TypeError: Cannot convert object to primitive value

Screencap of issue:
screen shot 2018-05-17 at 2 37 48 pm

If we update the JS tab to iterate until an explicitly defined integer on line 6, we can iterate correctly, with no error:

var x = document.getElementById("list").childElementCount;
console.log(x, typeof x);
var num = 4;
console.log(num, typeof num);
for (i = 0; i < num; i++) {
     console.log(i);
}

Working results:
screen shot 2018-05-17 at 2 37 40 pm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions