I am working on a function that dynamically creates new divs based on a specific condition. Once these divs are created, I store them in an array using the .push() method like so:
function SnakeBody(){
BodySnake = document.createElement("div");
tabuleiro.appendChild(BodySnake);
BodySnake.classList.add("snakeBody");
StorePositions.push(BodySnake) ;
}
Later on, when another condition is met, I attempt to change a style element on each of the stored divs using forEach() method on the array they were stored in, as shown below:
function SnakeBodyLeft(){
StorePositions.forEach(element => {
BodySnake.style.gridRowStart = (Initial_y + y);
BodySnake.style.gridColumnStart = (Initial_x + x)+1 ;
});
};
The main idea behind this code is:
1 - Using forEach() to iterate through each element in the array.
2 - The function should then update the value/style of every element in the array.
Unfortunately, this functionality is not working as expected and I am struggling to identify what might be missing. Any help would be greatly appreciated!