I'm trying to create an array and storing data in the array every time a button is pushed.
const allButtonsToStore = document.getElementsByClassName("button");
let reason = [];
for (x=0; x < allButtonsToStore.length; x++) {
allButtonsToStore[x].addEventListener("click", function(){
/*reason.push([x]); not working*/
reason[x] = this.textContent;
console.log(reason[x]);
});
}
So what I want the output to be: reason0 = some textContent, reason1 = some textContent, reason2 = some textContent, etc.
But this is not working. When, in chrome, I console log reason[0], it outputs "undefined". When I log reason[20] it outputs the current content of the button clicked. reason[20] will get overwritten when another button is pushed. console.log(reason[x]); does correctly output the current button clicked.
It looks like the array is not working. When I add console.log([x]); to the script in the for loop, it does output all the numbers individually. I must be making a newbie mistake.