Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:41:21+00:00 2026-06-14T13:41:21+00:00

I am working on a counter app that uses mustache.js to create multiple counters

  • 0

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();
  });

});​
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:41:22+00:00Added an answer on June 14, 2026 at 1:41 pm

    Lets try to break this into sections. First of all you’re incrementing the value of all counters because the selector used to get the counter you’re after affects all counters, ergo the behaviour is the correct one. Let me explain:

    When you are doing this: var EachContainer = $("div.valueCount"), the object jQuery returns is all elements matching that selector so if there is more than one counter, all will be affected. To fix this you can use the this keyword to move up the DOM tree from the button you’ve clicked, and select the only sibling with that selector.

    $(".add").live("click",function(){
            var counter = $(this).siblings(".valueCount");
            counter.html(parseInt(counter.text())+1);
            save();
        });
    

    This approach solves the issue you were likely experiencing by using .click since that one only affects the elements created at that given time and not future ones. With this little change you now have the possibility of creating multiple counters and saving their values. But not the titles, yet

    The first part is to invoke the save method on every modification of the title, although this could be done on other events such as the onChange event, but since that could potentially lead to loosing a title on some circumstances and the title length should be relatively small, you should be fine with just doing:

    $(".title").live("keyup",function(){
       save(); 
    });
    

    The second part is fairly simple and comes from the inability of some browsers to change the DOM when we dynamically modify a form attribute, meaning although the value of your input changes, the DOM doesn’t reflect it and therefor when you save the contents on the div containing all your counters, you are not saving the value attribute. To fix this, you end up with:

    $(".title").live("keyup",function(){
        $(this).attr('value',this.value);
        save(); 
    });
    

    Note that I’m not going to modify any of your other code or markup, and I’ve surrounded my code in a fork of your fiddle and placed it at the top of the Javascript section for easier location. Here’s the working fiddle, with the ability of adding new counters, changing the values of those counters and their titles and of course the option of deleting them (which was already working).

    Enjoy!


    Sources:

    • Firefox not updating DOM
    • The updated fiddle
    • jQuery live method
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a MFC project with Kinect that uses a FPS counter.
I have a counter in my app that has a reset button, but instead
Am using an NSMutableSet that is working great, up until this weird SIGABRT error
I am working on a web-app with ASP.NET MVC3. We are using nHibernate with
I am working on an app that has a rotating image (the user tapps
I'm working on a Rails 3 app that allows users to follow movies by
I'm trying to write a web app that uses Javascript to perform a fairly
I am working on a silverlight web app. It interacts with a module that
I am working on a web-services data processing app and I am trying to
I'm working with an app that processes device motion events and updates interface in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.