Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 9 years ago

Problem with javascript code?

I want when I click the button that it adds 1 to the variable money.

Instead when I click the Money Me button, it goes to $4 but the button disappears. How can I make the button stay?

Here's my code (I only started Javascript today so it may be very messy lol):

<html>

<head>

<script type="text/javascript">

var money =3

</script>

</head>

<script type="text/javascript">

function Party()

{money++;

document.write('Hi I have $' + money)

}

</script>

<button onclick='Party()'>Money Me!</button>

<body>

<script type="text/javascript">

document.write('Hi I have $' + money)

</script>

</body>

</html>

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    This is because the document.write() function will erase everything on the page then output. Therfore the button, even the script would get removed.

    You will have to find another way to ouput the value. (such as a prompt box)

  • 9 years ago

    i have used input box and removed document.write.This can be doen only if there is no

    restriction in using the input box.

    Working code(note that there is only one script body):-

    <html>

    <head>

    <script type="text/javascript">

    var money =3

    function Party()

    {money++;

    str="Hi I have $" + money;

    document.getElementById('in').value=str \\

    }

    </script>

    </head>

    <body>

    <button onclick='Party()'>Money Me!</button>

    <input type="text" id="in" value="Hi I have $3">

    </body>

    </html>

  • Anonymous
    4 years ago

    you're not to any extent further putting the cost for the variable numericalgrade any the position. try including real the following line after "var lettergrade;" numericalgrade = eighty 5; Then replace the above line to the different grade to get a diverse fee.

Still have questions? Get your answers by asking now.