I'm trying to dynamically create buttons on a page for a random selection process based on predefined sets of data, but it pulls back a string (ex. "SouthProvinceA2") instead of from before when I was manually passing in the parameter (area name) in the HTML button onclick=generate(SouthProvinceA2)
Is it possible to convert the string to a variable name for this purpose? Thanks!
const areas = [
"Starter",
"PocoPath",
"SouthProvinceA2"
]
const SouthProvinceA2 = {
monsters: ["Monster1", "Monster2", "Monster3"]
}
areas.forEach(area => { //...
const newDiv = document.createElement("div");
const newButton = document.createElement("button");
newButton.onclick = function() {
var result = document.getElementById("result");
numResult = numResultGen(0, area.monsters.length) //this is where it errors
namedResult = area.monsters[numResult]
result.innerHTML = area.monsters[numResult]
setter(namedResult)
I tried looking up other solutions and using eval() and assigning it to window, but I couldn't out how to get both of them to work and they had other errors that seemed further down the wrong path.