Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Heres a script I have written for m 1

Status
Not open for further replies.

Seafury888

Programmer
Jun 3, 2003
11
CA
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)
Code:
<script type=&quot;text/javascript&quot;>
	<!--
	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 &quot;sum&quot; 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(&quot;You Must Enter a Number for &quot;+ form1.elements[i].name);
				form1.elements[i].value = &quot;&quot;;
				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>
The form I'm using is named form1:
Code:
<form method=&quot;POST&quot; name=&quot;form1&quot; action=&quot;--WEBBOT-SELF--&quot;>
...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
 
Probably a number of issues here:
[tt]check[/tt] seems to be a reserved word. I had to change the function name to [tt]checkIt[/tt] to get it to work.

Secondly, your function requires an argument, if you were just to call [tt]check()[/tt] (or [tt]checkIt()[/tt]) the function will interpret form1 as a missing argument rather than the name of the form you wish to check. Better to set up your function like:
[tt]function checkIt(obj){[/tt]

Change all the references in the function from 'form1' to 'obj' and call the function as:
[tt]checkIt(document.form1);[/tt]

Should work a treat.
 
Worked like a charm, after I fixed a few logic errors as well...>)

Thanks a million

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top