...

View Full Version : Object is null or undefined error



orlandomm
11-07-2012, 07:15 PM
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?

<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>

Philip M
11-07-2012, 07:29 PM
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?

xelawho
11-07-2012, 07:32 PM
your updateNumber function accepts the argument cell

function updateNumber(cell)
but when you call it you do not pass anything

<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:

<input class=myButton type="button" value="1" onclick="updateNumber(this)">

orlandomm
11-07-2012, 07:51 PM
your updateNumber function accepts the argument cell

function updateNumber(cell)
but when you call it you do not pass anything

<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:

<input class=myButton type="button" value="1" onclick="updateNumber(this)">

Oh wow. I can't believe I forgot that part. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum