I'm trying to make a counter, that can only go from 0, to a max of 10, right now I can add and delete number from the counter, but for some reason my if/else never works, no matter where I put them. I'm a real noob to javascript so if anyone could give me some advice it would be highly appreciated.
Here's my code:
<script type="text/javascript">
var clicks = 0;
if (clicks < 0) {
clicks = 0;
document.getElementById("ticketAmmount").innerHTML = clicks;
}
function onClick() {
clicks += 1;
document.getElementById("ticketAmmount").innerHTML = clicks;
};
function offClick() {
clicks -= 1;
document.getElementById("ticketAmmount").innerHTML = clicks;
}
i also have tried this: but then the counter gives me NaN
<script type="text/javascript">
var clicks = 0;
function onClick() {
if (clicks < 0) {
clicks = 0;
document.getElementById("ticketAmmount").innerHTML = clicks;
} else {
clicks += 1;
document.getElementById("ticketAmmount").innerHTML = clicks;
}
};
function offClick() {
clicks -= 1;
document.getElementById("ticketAmmount").innerHTML = clicks;
}
</script>
clicks < 0right after setting it to0?if/else?ifstatement you're showing in the code block is any indication of where you're trying to put yourif/elseblocks, they'll only ever run once. You need to put them in your functions if you want them to run more than once.