Skip to content

Instantly share code, notes, and snippets.

@dengjonathan
Last active December 8, 2016 20:29
Show Gist options
  • Save dengjonathan/9cc3c4057ed03761a814e2558fe07bb1 to your computer and use it in GitHub Desktop.
Save dengjonathan/9cc3c4057ed03761a814e2558fe07bb1 to your computer and use it in GitHub Desktop.
Vanilla DOM counter
<!DOCTYPE html>
<html>
<head>
<title>Vanilla JS MVC</title>
</head>
<body>
<div class="counter">
<button id="up">+</button>
<p id="count"></p>
<button id="down">-</button>
</div>
<script>
(function() {
// MODEL: where we store all the data
let value = 0;
// VIEW: DOM nodes representing what is displayed on page
const $count = document.getElementById('count');
const $up = document.getElementById('up');
const $down = document.getElementById('down');
// CONTROLLER: how user interaction translates to model changes
$up.onclick = () => {
value = value + 1;
$count.innerHTML = value;
};
$down.onclick = () => {
value = value - 1;
$count.innerHTML = value;
};
// init page with current value
$count.innerHTML = value;
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment