1

Hey guys am new to javascript.I was wondering about how can i get the number of clicks in a paragraph with the onClick method in javascript.

The code i have used is

<html>
<body>
<p id="avalue"> kdfbkjdf </p>
<script>


var a = document.getElementById('avalue');
a.onClick = function(b) {
console.log(b);
}


</script>
</body>
</html>

This code doesnt show any errors but when i clicked the paragraph i didnt get the clicked numbers.

How can i acheive with onclick method ??..

Any help would be appreciated

2
  • 1
    You need a counter variable. Commented Nov 26, 2014 at 16:09
  • 1
    You have a typo. Your onClick should be onclick. Commented Nov 26, 2014 at 16:11

4 Answers 4

5

You need to maintain an external variable to work as a counter and increment it in the handler

var clicks = 0; // counter 
var a = document.getElementById('avalue'); // element
a.onclick = function(b) { // onclick not onClick
   console.log(++clicks); // increment it
}

From your comment, you could do this

a.ondblclick = function(){
   alert(clicks); // double click to know the number of clicks so far
}
Sign up to request clarification or add additional context in comments.

2 Comments

but i get the value 0 instead of their sum
@lovemysql click for sometimes and then double click
4

Save your click counter in the element's data to avoid the "need" for a global variable:

document.getElementById('avalue').addEventListener('click', function (event) {
    var element = event.currentTarget;
    element.clicks = (element.clicks || 0) + 1;
    console.log(element.clicks);
});

and access it anytime under document.getElementById('avalue').clicks.

5 Comments

I think it's a good idea to store the primitive data on the element, but you should probably put it on the bound element instead of the event.target just in case he adds nested elements.
Thanks. I'll change it to currentTarget.
Good call with the addEventListener, hon2a
@hon2a wow really thanx ..can you tell me how can i find the total of the onclick ..suppose if i clicked one time it shows 1 and if i clicked again it shows two..how can i get the whole sum ??..any ideas
The number of clicks is (in my code sample) saved in the click property right on the HTMLElement. So you can get it anytime by document.getElementById('avalue').clicks.
1

You need a counter that you increment each time it's clicked:

var b = 0;
var a = document.getElementById('avalue');
a.onclick = function() {
console.log(++b);
}

3 Comments

wow really thanx ..can you tell me how can i find the total of the onclick ..suppose if i clicked one time it shows 1 and if i clicked again it shows two..how can i get the whole sum ??..any ideas
That's what b will be.
@lovemysql: If you're not sure, then I really recommend taking a step back and reading through some basic JavaScript tutorials. If you just spend a couple days doing that, you'll have the majority of your questions answered. Eloquent JavaScript ... Learn JavaScript
0
var a = document.getElementById('avalue');
var b = 0;
a.onClick = function(b) {
    b++;
    console.log(b);
}

Change it to this and you should be able to see the number.

Comments

Your Answer

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.