2

I'm trying to write the variable "current" to the div "bank" using the following code when the user clicks on the image: document.getElementById('bank').innerHTML = current

According to my research, this should work, but it doesn't. Here's my code:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="css/default.css">
        <script>
            var current = 0;
            var totalThisGame = 0;

            function onButtonClick
            {
                current = current +1;
                totalThisGame = totalThisGame +1;
                document.getElementById('bank').innerHTML = current;
            }
        </script>
    </head>
    <body>
        <div id="left">
            <div id="bank">
            </div>
            <img id="image" src="img/image.png" alt="image" onclick=onButtonClick();>
        </div>
        <div id="middle">
        </div>
        <div id="right">
        </div>
    </body>
</html>

9 Answers 9

4

you miss the () behind your function

function onButtonClick()
            {
                current = current +1;
                totalThisGame = totalThisGame +1;
                document.getElementById('bank').innerHTML = current;
            }

your code is working fine for me

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

3 Comments

Thanks! I forgot to use them... how stupid.. :p It works fine now ;)
@MichaSprengers you should also accept this answer. This is how StackOverflow works.
I know and I will, I just had to wait for 10 minutes for some reason... So in a minute this will be accepted ;) Thanks for pointing it out tho
1

error

change  function onButtonClick ->  function onButtonClick()

<img id="image" src="img/image.png" alt="image" onclick=onButtonClick();> to

<img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">

1 Comment

Thanks that helped! But it doesn't matter if I use quotes around the onclick tho.. it works both ways..
0

Try:

<img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">

1 Comment

If you could write that you wrote quotations, that would make us understand your proposal better :)
0

Your function is badly defined. It should be function onButtonClick() instead of function onButtonClick. Then use that HTML code : <img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">

Comments

0

you forgot the function parenthesis

       function onButtonClick()
        {
            current = current +1;
            totalThisGame = totalThisGame +1;
            document.getElementById('bank').innerHTML = current;
        }

Comments

0

Bank has to be in double quotes: document.getElementById("bank").innerHTML = current;

Comments

0

Always use any of the functions on any event in html within double quotes.Try this

Comments

0

You should read the console message and you'd notice that your function is odd. Note parenthesis.

function onButtonClick
{

becomes

function onButtonClick()
{

Comments

0

change

function onButtonClick to  function onButtonClick()

<img id="image" src="img/image.png" alt="image" onclick=onButtonClick();>

to

<img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">

it would be better if you put your javascript before the body closing tag or use window.onload method

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.