I'm working on a clicker game in HTML, JS and CSS but when I go to do a for loop and set onclick, the onclick function only works for the last object. I'm using JSON data.
JS:
const Shop = document.getElementById("Shop")
const Upgrades = JSON.parse(json_upgrades)
let IncreasePerClick = function(Amount, ID) {
console.log("Debug!", ID)
let CurrentPerClick = Number(localStorage.getItem("PerClick") || 1)
let CurrentBalance = Number(localStorage.getItem("Cheese") || 0)
let Default = Number(document.getElementById(`Buy${ID}`).getAttribute("default"))
let CostUp = Number(localStorage.getItem(`Buy${ID}CostUp`))
let Cost = Number(localStorage.getItem(`Buy${ID}Cost`) || Default)
console.log(Cost)
if(CurrentBalance >= Cost){
CurrentBalance -= Cost
CurrentPerClick += Amount
Cost += CostUp
localStorage.setItem(`Buy${ID}CostUp`, CostUp)
localStorage.setItem("Cheese", CurrentBalance)
localStorage.setItem("PerClick", CurrentPerClick)
localStorage.setItem(`Buy${ID}Cost`, Cost)
}
}
for(let i = 0; i < Upgrades.length; i+=1){
Shop.innerHTML += `<div class="UpgradeHolder">
<span class="UpgradeName">${Upgrades[i]["name"]}</span>
<button class="UpgradeButton" id="Buy${i}" amountup="${Upgrades[i]["amount"]}" default="${Upgrades[i]["default"]}" costup="${Upgrades[i]["costup"]}" itemtype="${Upgrades[i]["type"]}">Buy for ${Upgrades[i]["default"]}</button>
</div>
`
console.log("Added div!")
let NewUpgradeButton = document.getElementById(`Buy${i}`)
let UpgradeType = NewUpgradeButton.getAttribute("itemtype")
let CostUp = Number(NewUpgradeButton.getAttribute("costup"))
let Default = Number(NewUpgradeButton.getAttribute("Default"))
let AmountUp = Number(NewUpgradeButton.getAttribute("amountup"))
if(UpgradeType == "ClickUpgrade"){
NewUpgradeButton.onclick = function(){
IncreasePerClick(AmountUp, i)
UpdateBalance()
}
console.log("Set Onclick!", i, AmountUp)
}
}
JSON (in js file):
const json_upgrades = `[
{"name": "Clickbot", "default": 50, "type": "ClickUpgrade", "amount": 1, "costup": 10},
{"name": "Click Machine", "default": 125, "type": "ClickUpgrade", "amount": 3, "costup": 15}
]`
When I click on any button other than the last one it doesn't do anything, how can I fix this? I can provide screenshots.
Screenshot in this screenshot it shows the 2 buttons, on the right the console says "Debug! 1", 1 being the ID of the 2nd button. That is said as a result of pressing the 2nd button.
When I press the first button, it does nothing.
Edit: I got it to work after trying a few different things!