I'm attempting to generate dynamic buttons based on an object:
var buttonsMenu = {
"A":{
A1: 97,
A2: 85,
A3: 65,
A4: 70,
A5: 40
},
"B":{
B1: 97,
B2: 85,
B3: 65,
B4: 70,
B5: 40
}
};
var categoryButton = "A";//or B
// create a button for each category
for(property in buttonsMenu.categoryButton){// facing challenges here
var newEl = document.createElement('button');
newEl.innerText = property;
newEl.setAttribute('data-name', property);
buttons.appendChild(newEl);
This approach works with the string value A or B in the loop:
for(property in buttonsMenu.A)
I expected the same behavior by using a variable, but it doesn't work.:
var categoryButton = "A";
for(property in buttonsMenu.categoryButton){
What am I doing incorrectly here? Appreciate your help!