I have got an unexpected behavior from my Javascript code. I'm creating a line of table with document.createElement("tr") and an array of cells with that code:
cellule = new Array(3).fill(document.createElement("td"));
But when I'm filling it with my values using innerHTML property case by case, the whole array is modified. Here is full code:
ligne = document.createElement("tr");;
cellule = new Array(3).fill(document.createElement("td"));
cellule[0].innerHTML = "Chat";
cellule[1].innerHTML = "Chien";
cellule[2].innerHTML = "Alligator";
ligne.appendChild(cellule[0]);
ligne.appendChild(cellule[1]);
ligne.appendChild(cellule[2]);
maTable.appendChild(ligne);
Results are :
cellule[0] => "Alligator"
cellule[1] => "Alligator"
cellule[2] => "Alligator"
Why is my whole array filled with the last innerHTML used?
tdelement here. You stuffed three references to the element into your array, but it is still only one element.new Array(3).fill(document.createElement("td"));you add a single<td>element in three different positions in one array.