I'm struggling to figure out how to set a maximum limit for a counter in my onclick event. Can someone help me?
- What is the best way to add a max limit to the counter of an onclick event?
- Do I need to use an
if
statement? If yes, how should it be written? - Should any changes be made to the html section? If so, what needs to be changed?
- If someone could provide an example using my function/variable names like (
goldClick
,gold
) it would be very beneficial. - In case a different function/variable is needed, please include
silver
andcopper
in the name for clarity purposes.
var gold = 0;
function goldClick(){
gold = gold + 1;
document.getElementById("gold").innerHTML = gold;
};
<button type="button" onclick="goldClick()">Gold</button>
<p>Gold:<a id="gold">0</a>/50</p>