Last active
December 8, 2016 20:29
-
-
Save dengjonathan/9cc3c4057ed03761a814e2558fe07bb1 to your computer and use it in GitHub Desktop.
Vanilla DOM counter
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
<!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