Struggling to set up a system for Parent and Child rows in an HTML table. While the code is somewhat functional, my lack of JavaScript knowledge is causing issues with selecting elements correctly. When I click on the first parent row, only one child row appears instead of both. And when I click on the second parent row, it displays the first child row of the first parent.
I have a strong feeling that the problem lies within the JavaScript portion of the code.
var toggler = document.getElementsByClassName("parent");
var i;
for (i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
this.parentElement.querySelector(".child").classList.toggle("active");
this.classList.toggle("parent-down");
this.parentElement.querySelector(".arrow").classList.toggle("arrow-down ");
});
}
.parent {
cursor: pointer;
user-select: none;
/* Prevent text selection */
font-size: 16px;
}
.arrow-down::before {
-ms-transform: rotate(90deg);
/* IE 9 */
-webkit-transform: rotate(90deg);
/* Safari */
'
transform: rotate(90deg);
}
.parent-down {
border: 2px solid rgb(21, 67, 96);
font-weight: bold;
}
/* Hide the child list */
.child {
display: none;
background-color: rgb(240, 250, 255);
font-size: 14px;
}
.active {
display: table-row;
}
.arrow::before {
content: "\25B6";
color: black;
display: inline-block;
margin-right: 6px;
}
<table>
<tr>
<th>Word</th>
<th>Number of Letters</th>
<th>Do I like the word?</th>
</tr>
<tr class="parent">
<td class="arrow">Long Words</td>
<td>-</td>
<td>-</td>
</tr>
<tr class="child">
<td>Bamboozle</td>
<td>9</td>
<td>Yes.</td>
</tr>
<tr class="child">
<td>Peritoneum</td>
<td>10</td>
<td>No.</td>
</tr>
<tr class="parent">
<td class="arrow">Short Words</td>
<td>-</td>
<td>-</td>
</tr>
<tr class="child">
<td>Squeak</td>
<td>6</td>
<td>Yes.</td>
</tr>
</table>