Created
August 4, 2018 17:15
-
-
Save subeshb1/3fca7cd0c7ba535216da2060b13cb38c to your computer and use it in GitHub Desktop.
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<div id="counters"> | |
<button id="btn">Add Counter</button> | |
</div> | |
<script> | |
const generateNewCounter = () => { | |
let init = 0; | |
const counterDisplay = document.createElement("h2"); | |
counterDisplay.textContent = init; | |
const counterIncrement = document.createElement("button"); | |
counterIncrement.textContent = "+"; | |
counterIncrement.onclick = () => { | |
init++; | |
counterDisplay.textContent = init; | |
} | |
const counterDecrement = document.createElement("button"); | |
counterDecrement.textContent = "-"; | |
counterDecrement.onclick = () => { | |
init--; | |
counterDisplay.textContent = init; | |
} | |
const counter = document.createElement("div"); | |
counter.appendChild(counterDisplay); | |
counter.appendChild(counterIncrement); | |
counter.appendChild(counterDecrement); | |
return counter; | |
} | |
const counterNode = document.getElementById('counters'); | |
const counterAdd = document.getElementById('btn'); | |
counterAdd.onclick = () => { | |
counterNode.appendChild(generateNewCounter()); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment