Skip to content

Instantly share code, notes, and snippets.

@gauravtiwari
Created February 19, 2017 11:59
Show Gist options
  • Save gauravtiwari/3217a4c145b802cc73d17279f94b81e7 to your computer and use it in GitHub Desktop.
Save gauravtiwari/3217a4c145b802cc73d17279f94b81e7 to your computer and use it in GitHub Desktop.
A simple counter example using vanilla JS
// 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