I'm new to node js. When a player connects I want the current list of rooms to be sent to them and then a list to be built that when an entry in that list is clicked it allows the player to join that room. For some reason the function only gets applied to the last element in the array. It doesn't matter how large it is, the player can only join the most recently added room. I added a console log statement to see if it's a server side problem but the 'test' statement does not get logged when clicking any div besides 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");
}
}
});