0

I found the original posted question about a simple jquery counter at Javascript, increment a counter on button click. I have borrowed the final solution for the script, and it works very easily, just like in the demo through JSFIDDLE. However, I am running into a slight problem. I need three individual counters, that work separately from one another. Can someone please help me work with this existing script function, to create the three multiple counters I need? Thank you for your help in advance.

1
  • 1
    Have you attempted to find a solution on your own? Commented Apr 11, 2013 at 16:48

1 Answer 1

0

See if this is what you want.

http://jsfiddle.net/H63As/30/

//wrote the code down here just in case...

html

<div>
    <div id="counter1">0</div>
    <div id="counter2">0</div>
    <div id="counter3">0</div>
    <button class="add" type="button" val="1">Add</button>
    <button class="add" type="button" val="2">Add</button>
    <button class="add" type="button" val="3">Add</button>
</div>

javascript

$(function () {
          $(".add").on("click", function () {
              $("#counter" + $(this).attr("val")).text(Number($("#counter" + $(this).attr("val")).text()) + 1);
          });

      });
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the quick responses. @Vivin Paliath, I did try to work with the script, but working with scripts is one of my weak areas; which is why I posted my question in the first place. I am very sorry that you feel that this was not a worthy question.
your solution worked perfectly. Thank you so much for your kindness and for taking the time to help me.
You're welcome. Please click the up arrow so that people can see this question has been resolved. :)
I am afraid that I am unable to do so. I keep getting an error message that I have to have a reputation of 15.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.