Skip to content

Instantly share code, notes, and snippets.

@subeshb1
Created August 4, 2018 17:15
Show Gist options
  • Save subeshb1/3fca7cd0c7ba535216da2060b13cb38c to your computer and use it in GitHub Desktop.
Save subeshb1/3fca7cd0c7ba535216da2060b13cb38c to your computer and use it in GitHub Desktop.
<!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