0

I am making a game with javascript where you can buy fish and I am trying to figure out how to make it so that when function agf() is executed the number in <p id="money">10</p> is subtracted by 2. So far I have tried:

var m=document.getElementById('money').value;
function agf() {
  if(m>1) {
    var pay=(m-2);
    document.getElementById('money').innerhtml=pay;
1
  • I am trying to do the same thing with addition now but it is not working. I tried switching var pay=(m-2); to var sell=(m+2); and then document.getElementById('money').innerHTML=pay; has sell at the end instead of pay. Commented Oct 26, 2013 at 15:21

1 Answer 1

2
  • Fetching the current value should be done inside the function so that you're always getting the current value.

  • The .value should be .innerHTML or .textContent.

  • To be safe, you should convert the value to a number.

  • The second .innerhtml should be innerHTML (notice the capitalization).

  • You're missing closing brackets


function agf() {
    var m=Number(document.getElementById('money').innerHTML);
    if(m>1) {
        var pay=(m-2);
        document.getElementById('money').innerHTML=pay;
    }
}
Sign up to request clarification or add additional context in comments.

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.