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 Answer
See if this is what you want.
//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);
});
});
4 Comments
user2271249
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.
user2271249
your solution worked perfectly. Thank you so much for your kindness and for taking the time to help me.
Shazboticus S Shazbot
You're welcome. Please click the up arrow so that people can see this question has been resolved. :)
user2271249
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.