We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,127 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

click counter

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>
3
Contributors
3
Replies
3 Hours
Discussion Span
5 Months Ago
Last Updated
4
Views
Question
Answered
ThePythonNoob
Light Poster
39 posts since Mar 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

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

urtrivedi
Posting Virtuoso
1,714 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

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>
JorgeM
Industrious Poster
4,017 posts since Dec 2011
Reputation Points: 294
Solved Threads: 548
Skill Endorsements: 115

Thanks alot clears things up alot.

ThePythonNoob
Light Poster
39 posts since Mar 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 5 Months Ago by urtrivedi and JorgeM

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0862 seconds using 2.65MB