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>

Recommended Answers

All 3 Replies

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

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>

Thanks alot clears things up alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.