1,105,214 Community Members

click counter

Member Avatar
ThePythonNoob
Light Poster
39 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, I am trying to make a click counter.
The prolem is the onClick part in button code.
If i made it so it was "click();" and placed the var clicks into the function click then it would kind of work.
But when I pass a variable into it.. it won't.
Thanks.

<html>
<head>
    <h1 align = center>Javascript Dump</h1>
</head>
<script type ="text/javascript">
var clicks = 0;
function click(clicks){
    x += 1;
    document.getElementById("counter").value = x;
}
</script>
//button code
<input type = "button", value = "0", onClick = "click(clicks);", id = "counter">
</html>
Member Avatar
urtrivedi
Posting Virtuoso
1,897 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 27 [?]
 
0
 

because when u pass "click" variable in fucntion, it overrides global "click". so do it without function variable.

Member Avatar
JorgeM
IT Addict
6,387 posts since Dec 2011
Reputation Points: 567 [?]
Q&As Helped to Solve: 956 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

For some reason, the function cannot be called click... its probably a keyword. Here is an updated version based on your code. Please note that some of your elements were in the wrong place and you were missing some structural HTML elements.

The sample below will increase the value and store it within the button's value property.

<!DOCTYPE html>
<html>
<head>
<script>
var x=0;
function clickMe(){
    x = x +1;
    document.getElementById("counter").value = x;
}
</script>    
</head>
<body>
<h1>Javascript Dump</h1>
<input id="counter" type="button" value="0" onclick="clickMe();" >
</body>
</html>
Member Avatar
ThePythonNoob
Light Poster
39 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks alot clears things up alot.

Question Answered as of 1 Year Ago by urtrivedi and JorgeM
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article