I noticed a strange problem when using Element.children and don't seem to find a good work-around.
Example 1 (expected behaviour)
Take this HTML:
<form>
<input name="hi">
<input name="bye">
</form>
And this JS
const formElement = document.querySelector('form');
for (let i = 0; i < formElement.children.length; i++) {
console.log(formElement.children[i].name);
}
The console prints the following, as you would expect:
"hi"
"bye"
Example 2 (strange behaviour)
Now, let's add an input field with name="children"
<form>
<input name="hi">
<input name="bye">
<input name="children">
</form>
The console prints nothing.
Why this happens in my opinion
formElement.children returns an HTMLCollection.
In an HTMLCollection you can access the child elements via an index, or directly by the name attribute of the element you are trying to target.
So in order to get the "hi" element, you could say formElement.children[0] or formElement.hi.
However when there is an element with the name "children" inside the collection, formElement.children will return the element with name="children", and there is no longer any way to loop trough all the elements.
How do I work around this?
That's my question. I know I can simply not use the name "children" and choose something else instead, but surely there must be a way to make this work?
Here's a pen to illustrate the problem: https://codepen.io/pwkip/pen/XWVoXVE
EDIT:
this seems to be related to the fact that formElement is a <form>. When I change it to a <div>, things work. So I guess the question boils down to this:
how do I convert a HTMLFormElement to regular HTMLElement?