0

Heres some of what I got, its a calculator, I'mg tryign to call the JS method from the button click events, something wrong though, display won't update.

<script language="javascript" type="text/javascript" src="calculator.js"> 
        var display = document.getElementById('display');

        function digit( num ){
            if(display == 0){
                display.innerHTML = num;
                } else {
                    display.innerHTML = display.innerHTML + "" + num;
                    }
        };


    </script>
    </head>
    <body>
    <form name="calculator">
      <table width="auto" border="1">
        <tr>
          <!--<td colspan=4><input id="display" type="text"></td> -->
          <td colspan=4><div id="display"></div></td>
        </tr>
        <tr>
          <td>MC</td>
          <td>MR</td>
          <td>MS</td>
          <td><input type="button" value='/' onclick="pushButton(this.value);"></td>
        </tr>
        <tr>
          <td><input type="button" value=7 onclick="javascript:digit(this.value);"></td>
          <td><input type="button" value=8 onclick="javascript.digit(this.value);"></td>
          <td><input  type="button" value=9 onclick="pushButton(this.value);"></td>
          <td><input  type="button" value='*' onclick="pushButton(this.value);"></td>
        </tr>
        <tr>
          <td><input  type="button" value=4 onclick="pushButton(this.value);"></td>
          <td><input  type="button" value=5 onclick="pushButton(this.value);"></td>
          <td><input  type="button" value=6 onclick="pushButton(this.value);"></td>
1
  • What's the pushButton function? Commented Feb 15, 2013 at 4:43

3 Answers 3

3

Move

var display = document.getElementById('display');

into your function.

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

Comments

2

You are calling document.getElementById('display'); before that element (id=display) exists. You can do a variety of things, but the easiest would just be to move the <script> to just before </body>, or at least after id=display

Comments

0

Place 0 in the div Or change the condition in script (display == null )

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.