I am wondering how to create JS counter (desired look below) using private properties on a prototype obeject and a closure. If someone could show me how but also comment it so I can understand the steps and process, I would really appreciate it.
<!DOCTYPE html>
<html>
<body>
<button name="button" class="click-tracking">Click Me</button>
<script>
var counts = {},
track = document.getElementsByClassName('click-tracking');
for (var i = 0, max = track.length; i < max; i++) {
track[i].addEventListener('click', function() {
var name = this.name,
ele = document.getElementById(name + '-count') || false;
if (typeof counts[name] === 'undefined') {
counts[name] = 0;
}
if (!ele) {
var ele = document.createElement('div');
ele.id = name + '-count';
this.parentNode.insertBefore(ele, this.nextSibling);
}
ele.innerHTML = counts[name]++;
});
}
</script>
</body>
</head>
</html>