I am working on a counter app that uses mustache.js to create multiple counters and remembers your data using local storage. You simply click the '+' button to add a new counter, and hit the add button to increase the counter.
I am having several issues: - When add, and have multiple counters, they all increase. - When I click the close button, the counters also increase.
Is it also possible to enter a title via the input field and have local storage remember it?
Any help would be greatly appreciated. If you have any solutions, can you explain?
See the fiddle here: http://jsfiddle.net/techydude/MSnuE/
Javascript:
$(function() {
var doc = $(document),
CounterContainer = $("#CounterContainer"),
//Container for all Counters
container = $(".counter"),
//Individual Conter Counters
counter = $(".valueCount"),
addBtn = $(".add"),
valueCount = $(".valueCount").html(),
addCounter = $("#create-counter"),
counterTemplate = Mustache.compile($("#counter-template").html());
if (localStorage.counterSave) {
CounterContainer.html(localStorage.counterSave);
}
function save() {
localStorage.counterSave = CounterContainer.html();
}
addCounter.submit(function(e) {
// prevent page from refresing
e.preventDefault();
var value = 5;
makeCounter(value);
save();
});
function makeCounter(value) {
$(counterTemplate({
txt: value
})).appendTo(CounterContainer);
}
CounterContainer.on("click", addBtn, function(valueCount) {
var EachContainer = $("div.valueCount"),
EachContainerValue = $("div.valueCount").html();
EachContainer.html(++EachContainerValue);
console.log("test");
save();
});
doc.on("click", "button.removeCounter", function() {
$(this).parent().remove();
save();
});
});