As someone who has recently started learning about node js, I am facing a challenge. When a player connects to the server, I want to send them the current list of available rooms and allow them to join a room by clicking on it. However, I'm running into an issue where the click function only works for the last element in the array. No matter how many rooms are added, the player can only join the most recent one. To troubleshoot, I added a console log statement but it doesn't get logged when clicking on any div other than the last one added.
socket.on('rooms', function (data) {
for (var i in data) {
roomsListDiv.innerHTML += '<div>' + data[i].roomNumber + '<div>';
RoomDivs[data[i].roomNumber] = (roomsListDiv.lastChild);
RoomDivs[data[i].roomNumber].style.cursor = 'pointer';
RoomDivs[data[i].roomNumber].onclick = function () { //join room
socket.emit('joinRoom', { room: data[i].roomNumber });
console.log("test");
}
}
});