0

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?

1
  • It's better to not use global variables and functions. Commented Jan 11, 2014 at 4:36

3 Answers 3

4

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;

}
Sign up to request clarification or add additional context in comments.

Comments

3

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;
}

Comments

0

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>

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.