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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is the field.length value of a checkbox undefined?

Status
Not open for further replies.

acoble

Programmer
Nov 30, 2000
6
US
Why does the script show a value of undefined when there is only one checkbox? It will show a length of 2 for two fields.

Code:
<HTML>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

function checkForNoSelection(field)
{
	var checked = false;
	alert(&quot;Field length is: &quot; + field.length);
	for (i = 0; i < field.length; i++) {
		if (field[i].checked == true) {
			checked = true;
		}
	}
	if (checked == false) {
		alert(&quot;Please make a selection.&quot;);
		return false;
	}
}

</script>

<HEAD>
</HEAD>

<BODY>
<form name=&quot;FileList&quot; method=&quot;post&quot; OnSubmit=&quot;return checkForNoSelection(document.FileList.filestr);&quot;>
	Select &quot;Do It&quot; and a pop-up shows the checkbox length is undefined.<br>
	<input name=&quot;filestr&quot; type=&quot;checkbox&quot;  value=&quot;R50602D.TRZ&quot;>r50602d.trz
	<br>
	<input name=submit type = &quot;submit&quot; value = &quot;  Do It  &quot;>
	<br>
	<input type=&quot;button&quot; name=&quot;CheckAll&quot; value=&quot;Check All&quot; onClick=&quot;checkAll(document.FileList.filestr)&quot;>
	<br>
	<input type=&quot;button&quot; name=&quot;UnCheckAll&quot; value=&quot;Uncheck All&quot; onClick=&quot;uncheckAll(document.FileList.filestr)&quot;>
	<br>
	<br>
</form>
<form name=&quot;FileList2&quot; method=&quot;post&quot; OnSubmit=&quot;return checkForNoSelection(document.FileList2.filestr);&quot;>
	Select &quot;Do It&quot; and a pop-up shows the checkbox length is 2.<br>
	<input name=&quot;filestr&quot; type=&quot;checkbox&quot;  value=&quot;R50602D.TRZ&quot;>r50602d.trz
	<br>
	<input name=&quot;filestr&quot; type=&quot;checkbox&quot;  value=&quot;R50505D.TRZ&quot;>r50505d.trz
	<br>
	<input name=submit2 type = &quot;submit&quot; value = &quot;  Do It  &quot;>
	<br>
	<input type=&quot;button&quot; name=&quot;CheckAll&quot; value=&quot;Check All&quot; onClick=&quot;checkAll(document.FileList2.filestr)&quot;>
	<br>
	<input type=&quot;button&quot; name=&quot;UnCheckAll&quot; value=&quot;Uncheck All&quot; onClick=&quot;uncheckAll(document.FileList2.filestr)&quot;>
</form>
</BODY>
</HTML>
[code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top