I am assigning event listeners inside of a loop. I am passing a variable that changes throughout the loop into the event listener function. However I am having a problem. All of the event listeners are receiving the last value of the variable in the loop, instead of the value that I meant to send at each point in the loop. The function where I assign the event listeners is the following
let arr;
let newElem;
let id;
  for (let i = 6; i < 13; i++) {
    arr = this.state.cards[i];
    if (arr.length > 1) {
      for (let j = 0; j < arr.length; j++) {
        if (j !== arr.length-1) {
          newElem = document.createElement('div');
          newElem.id = 'p' + i + '-' + (j+1);
          document.getElementById('p' + i + '-' + 
            j).appendChild(newElem);
        } else {
          id = 'p' + i + '-' + j;
          document.getElementById(id).className = 'cardLocations 
            faceUp';
          newElem = document.createElement('p');
          newElem.innerHTML = arr[j].id;
          document.getElementById(id).appendChild(newElem);
          document.getElementById(id).addEventListener('click', () => 
            {this.selectCardsFromBoard(id)});
        }
      }
    } else {
      newElem = document.createElement('p');
      newElem.innerHTML = arr[0].id;
      document.getElementById('p6-0').appendChild(newElem);
      document.getElementById('p6-0').addEventListener('click', () => 
        {this.selectCardsFromBoard('p6-0')});
    }
The problem is that whenever I click on any of the divs that I assigned event listeners to, the function selectCardsFromBoard is called with the same value and that is the final value that 'id' reaches in the loop. I want to be able to call the function with the value that 'id' is when it is assigned. I think this is some kind of pass by reference issue but I can't figure it out.
 
     
    