I've encountered a strange issue with my Javascript code. I'm trying to create a table row using document.createElement("tr")
and an array of cells like this:
cell = new Array(3).fill(document.createElement("td"));
However, when I populate the cells one by one using the innerHTML property, it seems to affect the entire array. Here's the code snippet:
row = document.createElement("tr");
cell = new Array(3).fill(document.createElement("td"));
cell[0].innerHTML = "Cat";
cell[1].innerHTML = "Dog";
cell[2].innerHTML = "Alligator";
row.appendChild(cell[0]);
row.appendChild(cell[1]);
row.appendChild(cell[2]);
myTable.appendChild(row);
The output shows:
cell[0] => "Alligator"
cell[1] => "Alligator"
cell[2] => "Alligator"
Why does assigning innerHTML to one cell change all cells in the array?