My code increments a number up or down by 1. The problem is "testNum" which is a global variable should reflect the increments up or down but when I check the value of "testNum" in the console it's always 20. I pass "testNum" to my function and return it. Why isn't it reflecting the increments?
Fiddle: http://jsfiddle.net/FnUXw/1/
<input type="button" id="btnDown" value="-">
<span id="amountDiv">amount div</span>
<input type="button" id="btnUp" value="+">
<script>
var testNum = 20;
amountDiv.innerHTML = testNum;
function makeButtonIncrement(button, upDown, displayDiv, variable) {
function changeValue () {
if (upDown == "up") {
variable++;
}
if (upDown == "down") {
variable--;
}
displayDiv.innerHTML = variable;
}
button.onclick = changeValue;
return variable;
}
makeButtonIncrement(document.getElementById('btnDown'), "down", document.getElementById('amountDiv'), testNum);
makeButtonIncrement(document.getElementById('btnUp'), "up", document.getElementById('amountDiv'), testNum);
</script>