Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Object is null or undefined error

    I'm working on a very simple javascript calculator and am having trouble with one error. Every time I click one of the numerical buttons, I get an error saying the value is null or undefined and I can't seem to find the problem. Any tips?
    Code:
    <html>
    <head>
    <title>test</title>
    <style type="text/css">
    	input.myButton {
    		background-color: maroon;
    		color: gold;
    		font-size: .25in;
    		font-family: "Courier New", "Courier";
    		font-weight: bold;
    		width: 1in;
    		height: .5in;
    	}
    </style>
    
    <script type="text/javascript">
    var total;
    var num1;
    var num2;
    var sum;
    var symbol;
    var current="0";
     
    function updateNumber(cell) {
         symbol = cell.value;
         document.SimpleForm.showWin.value = current+ symbol ;
         current = document.simpleForm.showWin.value;
    	}
    
    function clearMemory() {
       num1 = 0;
       num2 = 0;
       sum = 0;
       symbol = 0;
       current="";
       document.simpleForm.showWin.value = 0 ;
      
    }
    function startAdding(){
       
       num1 = current;
       current ="";
       document.simpleForm.showWin.value = 0;
      
    }
    function computeSum() {
                    num2 = document.simpleForm.showWin.value;
                    sum = parseFloat(num1)+parseFloat(num2);
                    document.simpleForm.showWin.value = sum ;
                    num1 = 0;
        num2 = 0;
        sum = 0;
        symbol = 0;
        current="";
    }
    </script>
    
    </head>
    <body>
    
    <form name="simpleForm">
    <input class=myButton type=text name="showWin" value="0" size=10 align=right> <br>
    <input class=myButton type="button" value="1" onclick="updateNumber()">
    <input class=myButton type="button" value="2" onclick="updateNumber()">
    <input class=myButton type="button" value="3" onclick="updateNumber()"> <br>
    <input class=myButton type="button" value="4" onclick="updateNumber()">
    <input class=myButton type="button" value="5" onclick="updateNumber()">
    <input class=myButton type="button" value="6" onclick="updateNumber()"> <br>
    <input class=myButton type="button" value="7" onclick="updateNumber()">
    <input class=myButton type="button" value="8" onclick="updateNumber()">
    <input class=myButton type="button" value="9" onclick="updateNumber()"> <br>
    <input class=myButton type=button value="+" onclick="startAdding()">
    <input class=myButton type=button value="0" onclick="updateNumber(this)">
    <input class=myButton type=button value="=" onclick="computeSum()">
    <input class=myButton type=button value="CLEAR" onclick="clearMemory()">
    
    </form>
    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by orlandomm View Post
    I'm working on a very simple javascript calculator and am having trouble with one error. Every time I click one of the numerical buttons, I get an error saying the value is null or undefined and I can't seem to find the problem. Any tips?
    Have you tried using your errror console?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,912
    Thanks
    56
    Thanked 545 Times in 542 Posts
    your updateNumber function accepts the argument cell
    Code:
    function updateNumber(cell)
    but when you call it you do not pass anything
    Code:
    <input class=myButton type="button" value="1" onclick="updateNumber()">
    so when you try to use cell.value the code does not know what you are referring to, and fails there

    from what I can tell, that part of your code will work using "this" as a parameter:
    Code:
    <input class=myButton type="button" value="1" onclick="updateNumber(this)">

  • #4
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    your updateNumber function accepts the argument cell
    Code:
    function updateNumber(cell)
    but when you call it you do not pass anything
    Code:
    <input class=myButton type="button" value="1" onclick="updateNumber()">
    so when you try to use cell.value the code does not know what you are referring to, and fails there

    from what I can tell, that part of your code will work using "this" as a parameter:
    Code:
    <input class=myButton type="button" value="1" onclick="updateNumber(this)">
    Oh wow. I can't believe I forgot that part. Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •