Last active
March 28, 2019 03:55
-
-
Save nielslange/55300d953f4415cbab0e6b7104d227f4 to your computer and use it in GitHub Desktop.
Increase and descrease value via button click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="number" id="number" value="0" /> | |
<button type="button" onclick="increaseValue()">+</button> | |
<button type="button" onclick="decreaseValue()">-</button> | |
<button type="button" onclick="resetValue()">Reset</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function increaseValue() { | |
var value = parseInt(document.getElementById('number').value, 10); | |
value = isNaN(value) ? 0 : value; | |
value++; | |
document.getElementById('number').value = value; | |
} | |
function decreaseValue() { | |
var value = parseInt(document.getElementById('number').value, 10); | |
value = isNaN(value) ? 0 : value; | |
value < 1 ? value = 1 : ''; | |
value--; | |
document.getElementById('number').value = value; | |
} | |
function resetValue() { | |
var value = 0; | |
document.getElementById('number').value = value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment