Seafury888
Programmer
Heres a script I have written for my website. Now, my manual says that "elements[]" is an array used by javascript to hold a form's elements, and "length" is an attribute that holds the number of elements in the form. So I'm wondering why I keep getting an error telling me that 'length' is null or not an object. I have also tried substituting the 'elements[]' array for length in the code below.
What I'm trying to do is basically use one text box to store the total of 4 other text boxes, with the data entered by the user. The button onClick event handler calls the function that calculates the total and checks for NaN values.(yes I suppose the function should only perform 1 action)
The form I'm using is named form1:
...and it definitely has text boxes and buttons in it, so elements[] and length should not be null in my opinion.
Any thoughts are greatly appreciated. This is my first time writing a real script so don't assume I know the obvious.
Thanks
Ian
What I'm trying to do is basically use one text box to store the total of 4 other text boxes, with the data entered by the user. The button onClick event handler calls the function that calculates the total and checks for NaN values.(yes I suppose the function should only perform 1 action)
Code:
<script type="text/javascript">
<!--
function check(form1)
{//check all the t-shirt text fields for non-numeric data
//there are 20 elements in the whole form. 17 text boxes, and 3 buttons. I'm only looking to deal with text boxes 13-16 (user entered values). 17 is the "sum" text box....I think. Those details can be worked out, the real problem is length and elements[] being null.
for (i=13; i<form1.length-2; i++)//<--why is length null?
{
if(isNaN(form1.elements[i].value))
{
alert("You Must Enter a Number for "+ form1.elements[i].name);
form1.elements[i].value = "";
form1.elements[i].focus();
return;
}
}
//all T-shirts contain numeric data
var total = 0;
for(i=13; i<form1.length-2; i++)
total = (total + parseFloat(form1.elements[i].value))*18.00;
//sum is the name of the text box that stores the total of the other text boxes.
form1.sum.value = total + 5.00;
}
-->
</script>
Code:
<form method="POST" name="form1" action="--WEBBOT-SELF--">
Any thoughts are greatly appreciated. This is my first time writing a real script so don't assume I know the obvious.
Thanks
Ian