Reputation: 1
I am using this function for a button:
points1 = 0;
function onclick1 (points1) {
points1 += 5;
document.getElementById("point1").innerHTML = points1;
}
Right now function only works on the first button click but not on subsequent clicks. How do I fix it?
Upvotes: 0
Views: 71
Reputation: 137
Try this
<!DOCTYPE html>
<html>
<head>
<script>
point1 = 0;
function myFunction()
{
point1 += 5;
document.getElementById("demo").innerHTML=point1;
}
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
</body>
</html>
Upvotes: 0
Reputation: 148980
You've declared points1
as a function parameter, so it hides the global variable of the same name. Remove that parameter and it should work:
var points1=0;
function onclick1(){
points1+=5;
document.getElementById("point1").innerHTML=points1;
}
Upvotes: 3
Reputation: 1768
You are passing points1 in as a var in the function. Try and change it to -->
points1=0;
function onclick1(){
points1+=5;
document.getElementById("point1").innerHTML=points1;
}
Upvotes: 4