Created
February 19, 2017 11:59
-
-
Save gauravtiwari/3217a4c145b802cc73d17279f94b81e7 to your computer and use it in GitHub Desktop.
A simple counter example using vanilla JS
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
// A simple counter example | |
// The setup will be more complicated in modern apps built using React | |
const incrementNode = document.getElementById('increment'); | |
const decrementNode = document.getElementById('decrement'); | |
const inputNode = document.getElementById('counter'); | |
const counter = { | |
initialize() { | |
incrementNode.addEventListener('click', (event) => { | |
event.preventDefault(); | |
const currentValue = inputNode.value; | |
inputNode.value = parseInt(currentValue, 0) + 1; | |
}); | |
decrementNode.addEventListener('click', (event) => { | |
event.preventDefault(); | |
const currentValue = inputNode.value; | |
if (currentValue > 0) { | |
inputNode.value = parseInt(currentValue, 0) - 1; | |
} | |
}); | |
} | |
}; | |
export default counter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment