3

I was wondering how I could increase a variable when a button is clicked. here is my code. Can somebody point me in the right direction? thanks.

I am thinking when the button1 is clicked it will increase team 1's score by like 10, and will decrease if it is clicked again if that is necessary.

var Team2;
var Team2 == 0;
var Team1;
var Team1 == 0;
document.getElementById("calc").innerHTML = Team1;



function clicked(button1)
{
var team1 = team1 + 45
}
</SCRIPT>
<p>
Players for Team 1
9
  • Shouldn't team1 = team1 be Team1? With a capital "T" Also I don't see anything calling clicked() Commented May 20, 2016 at 0:05
  • The same way you have called writeText() assuming you want the to be called when those buttons are clicked... Commented May 20, 2016 at 0:08
  • Try adding onClick="readText(this.form);writeText(); to your button inputs. Also where is the readText function? And why are you passing button1 into the writeText function but never using it? Can you display all relevant source code. Commented May 20, 2016 at 0:12
  • 1
    Just to be clear your question is out of sync with the title "radio button is clicked" those are not radio inputs. Commented May 20, 2016 at 1:20
  • 1
    @GerardoFurtado Yes, that comment was just to help instruct the OP on the question quality. Commented May 20, 2016 at 1:36

1 Answer 1

3

First, the variable part: suppose you define a variable var value = 0. To increase it by 10, you can write value = value + 10, but in JavaScript this can be shorten to:

value += 10

The same way, to decrease it, just write value -= 10.

To call the function, you write onClick="someFunction()" (not the best practice), and then you define the function:

function someFunction(){
  value += 10
};

This is the working fiddle: https://jsfiddle.net/gerardofurtado/6qh1yhsj/

If you want to see how to do the same thing without the onClick="someFunction()" part, here is a fiddle: https://jsfiddle.net/gerardofurtado/wff8mph3/

PS: I see that in your code you wrote var team2 == 0. In JavaScript, two equal signs make a comparison operator: http://www.w3schools.com/js/js_comparisons.asp

PS2: In JavaScript, you have to mind the scope. Once you defined the var team1, you can change it inside the function just by writing team1. But if you do as you did:

function someFunction(){
  var team1 = team1 + 45
};

This team1 is not the same previous variable team1 defined outside the function. It's a different variable. And, as the team1 to the right of the equal sign is not defined, this will return a NaN (Not-A-Number).

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

2 Comments

also you don't need to make a function to call another one on click event , you can simply make that document.getElementById("increase").addEventListener("click", increase);
Thanks, @El.oz, I just changed the fiddle.

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.