I'm trying to build a game based on a 2d grid in JavaScript.
Inside a nested for loop, I set the data attribute of a cell like this:
cell.setAttribute('data-pos',[i,j]);
In my event handler I get the data like this:
let pos = event.target.getAttribute('data-pos');
When I try to access the elements of pos
something strange happens.
For a cell with data-pos
of [1, 3]
,
I get the following values when I use console.log:
pos[0] = 1
pos[1] = ,
pos[2] = 3
Somehow the comma is being treated as an array element.
Why is this and how can I correctly pass values for i
and j
to the event handler please?