Within the following code, I am trying to get all .node
elements that are direct child elements of .row
elements. Currently my code will look for any given element that matches the .node
class within .row
element.
How could I get the direct node (e.g. in CSS this would look something like this: .row > .node
) in plain JavaScript?
let rows = document.getElementsByClassName("row");
var nodes = [];
for (let i = 0; i < rows.length; i++) {
console.log(rows[i].getElementsByClassName("node"));
}
.node {
border-radius: 50%;
border: 1px solid #000;
}
.node,
.empty {
display: inline-block;
height: 50px;
width: 50px;
line-height: 50px;
text-align: center;
}
.d-none {
display: none;
}
<div class="row">
<div class="node">
Foo
</div>
<div class="node d-none">
Foo
</div>
<div class="row">
<div class="empty">
</div>
<div class="node d-none">
Foo
</div>
</div>
</div>
The problem is that the final .node
element (which is nested in like so: .row > .row > .node
) will be called for multiple times because I am not addressing it as direct child.
I saw this answer which explains the use of document.querySelector
. Should I be using that for the purpose I am trying to reach here? Or Could I achieve the same by just using document.getElementsByClassName
?