0

I need some help with the click event, I'm trying to have an individual counter that is incremented by the click event that I have on the img. I've tried many variations, I want to resolve this without using jQuery.

 <script async>
 var count = 0;
 var clickerCount = document.getElementsByClassName('clicker');
 var cat = {
 count : 0,
 counter: function(){
 this.count++;
 clickerCount.textContent = "Kitten Click Count :" + this.count;
 console.log("counter function working");
 console.log(cat.count);
 }
 };
 function modifyNum(){
 cat.counter();
 console.log("modifyNum function working");
 }
 </script>
</head>
<body>
 <div style="display:inline">
 <div>
 <img src="http://placekitten.com/200/296" id="cat0" onclick="modifyNum();">
 <p id='clicker'>Kitten Click Count :</p>
 </div>
 <div>
 <img src="http://placekitten.com/200/296" id='cat1' onclick="modifyNum();">
 <p id='clicker'>Kitten Click Count :</p>
 </div>
 </div>
ecarrizo
2,80919 silver badges29 bronze badges
asked Jul 3, 2015 at 20:42

4 Answers 4

2

For a start, you are using id='clicker' in two places (IDs are supposed to be unique), and then using document.getElementsByClassName, which returns nothing because you used an ID and not a class.

Once you do change it to a class, document.getElementsByClassName will return an array of elements. You'll have to use clickerCount[0] and so on, or loop through the array.

answered Jul 3, 2015 at 20:49
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the help, although Im still unsure of how to increment the the count individually. Also I cant seem to get the actual count on the screen. Id like for cat0 and cat1 to have different counts.
1

This example should work. I've separated the HTML from the Javascript because it looks clearer for me. You can use it as an example to expand / create your own in your own way.

Hope it help

HTML:

<div style="display:inline">
 <div>
 <img src="http://placekitten.com/200/296" id="1" class="countable">
 <span>Kitten Click Count :</span><span id="counter-for-1">0</span>
 </div>
 <div>
 <img src="http://placekitten.com/200/296" id="2" class="countable">
 <span>Kitten Click Count :</span><span id="counter-for-2">0</span>
 </div>
</div>

JS:

var imagesCountable = document.getElementsByClassName("countable");
var counters = [];
for (var i = 0; i < imagesCountable.length; i++) {
 counters[imagesCountable[i].id] = 0;
 imagesCountable[i].onclick = function(e) {
 document.getElementById("counter-for-" + e.currentTarget.id)
 .innerHTML = ++counters[e.currentTarget.id];
 }
}

var imagesCountable = document.getElementsByClassName("countable");
var counters = [];
for (var i = 0; i < imagesCountable.length; i++) {
 counters[imagesCountable[i].id] = 0;
 imagesCountable[i].onclick = function(e) {
 var cElem = document.getElementById("counter-for-" + e.currentTarget.id);
 cElem.innerHTML = ++counters[e.currentTarget.id];
 }
}
<div style="display:inline">
 <div>
 <img src="http://placekitten.com/200/296" id="1" class="countable">
 <span>Kitten Click Count :</span><span id="counter-for-1">0</span>
 </div>
 <div>
 <img src="http://placekitten.com/200/296" id="2" class="countable">
 <span>Kitten Click Count :</span><span id="counter-for-2">0</span>
 </div>
 </div>

answered Jul 3, 2015 at 21:58

2 Comments

Thanks! This is good, I wish I could've came up with this on my own. Can you explain why you have the counters array wrapping the images countable.
@alphapilgrim it is about this line? counters[imagesCountable[i].id] = x?
1

I have solved this problem in this JSFiddle!

If you can hardcode the IDs then it's easier in my point o view to just manipulate things by ID.

 <div>
 <img src="http://placekitten.com/200/296" id="cat0" onclick="counter(0);">
 <p id='clicker0'>Kitten Click Count :</p>
 <input type="hidden" id="counter0" value="0">
 </div>
function counter(id) {
 var cnt = parseInt(document.getElementById("counter" + id).value);
 cnt++;
 document.getElementById("counter" + id).value = cnt;
 document.getElementById('clicker' + id).innerHTML = 'Kitten Click Count :' + cnt;
}

It's not the same approach but I find it easy to understand.

Hope it helps.

answered Jul 3, 2015 at 21:35

Comments

0

Ok, so first off you have two elements with the id of 'clicker'. You probably meant for those to be classes and ids. So when you call modifynum() it cant locate those because the class doesn't exists. Second, your JS is loading before your HTML elements. So when the JS gets to this line:

var clickerCount = document.getElementsByClassName('clicker');

It is going to find nothing, even if you correct the class names. So you want to move your JS to the footer of your HTML document, or wrap the code in a method that is called on pageLoad().

I think that should take care of it. Your object, for the most part, looks correct.

answered Jul 3, 2015 at 20:49

2 Comments

I forgot to mention the part @Sam brings in his answer about the array of clickerCount.
I tried writing an if statement to check if the click.event was for the specific cat and still not working. Im still unsure of how to increment the the count individually. Also I cant seem to get the actual count on the screen. Id like for cat0 and cat1 to have different counts. I tried making two variable for counters to increment. Still no go.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.