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

looping through array objects but only one object

Status
Not open for further replies.

unwonted

Technical User
Feb 9, 2009
3
IE
Hi all,

I have a created sort of a sub list of checkboxes with each of the checkboxes in the sublist having the same name but when I try to loop through any that only contain one checkbox I get an 'Expected Identifier' message.

I loop through the checkboxes when I'm validating if any checkboxes were selected using the .length attribute but that's where the problem lies.
It seems to be because when there is only one checkbox created for a sublist, an array isn't created and hence I can't use the arraay.length function.

Here's the sample tables with the list of checkboxes:
Code:
            <TABLE id="S00000001" border=1 BORDERCOLOR="#003366" cellpadding=1 cellspacing=0 STYLE="font-family: Arial, Helvetica, Sans-serif; color: black; font-size: 9pt">
             <TR>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Product<BR>Type</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Part<BR>Number</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Part<BR>Description</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Delivery<BR>Date</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Requested<BR>Boxes</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Cancel<BR><input type=checkbox name="completeSOChkBox" onclick="javascript:selDesel('<%=Rs("request number")%>', this)">
              </TD>
             </TR>
	     <TR>
              <TD align=center>Product 1</TD>
              <TD align=center>Part 2</TD>
              <TD align=center>Part Description 2</TD>
              <TD align=center>11-Mar-09</TD>
              <TD align=center>2</TD>
              <TD align=center><input type=checkbox name="S00000001partChkBox"></TD>
             </TR>
              <TD align=center>Product 2</TD>
              <TD align=center>Part 3</TD>
              <TD align=center>Part Description 3</TD>
              <TD align=center>11-Mar-09</TD>
              <TD align=center>3</TD>
              <TD align=center><input type=checkbox name="S00000001partChkBox"></TD>
             </TR>
            </TABLE>


            <TABLE id="S00000002" border=1 BORDERCOLOR="#003366" cellpadding=1 cellspacing=0 STYLE="font-family: Arial, Helvetica, Sans-serif; color: black; font-size: 9pt">
             <TR>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Product<BR>Type</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Part<BR>Number</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Part<BR>Description</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Delivery<BR>Date</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Requested<BR>Boxes</TD>
              <TD align=center STYLE="font: 10pt Verdana; color: #003366; font-weight: bold">Cancel<BR><input type=checkbox name="completeSOChkBox" onclick="javascript:selDesel('<%=Rs("request number")%>', this)">
              </TD>
             </TR>
	     <TR>
              <TD align=center>Product 4</TD>
              <TD align=center>Part 6</TD>
              <TD align=center>Part Description 6</TD>
              <TD align=center>12-Mar-09</TD>
              <TD align=center>16</TD>
              <TD align=center><input type=checkbox name=S00000001partChkBox"></TD>
            </TABLE>


           </TD>
          </TR>
         </TABLE>

and the javascript I'm using to try and loop through and validate:
Code:
	function cancelorderValidate(form) {

		// Define the required variables for the function
		var numOpenRequests = form.completeSOChkBox.length;		// number of open Sample Order requests
		var validOrderRequest = new Boolean();				// at least one sample requested
		var partialSampleCancel = new Boolean();			// partial sample order cancellations
		validOrderRequest = false;

		// Loop through the orders and check if any complete sample orders have been cancelled
		for (i = 0; i < numOpenRequests; i++) {

 			// Check to see if the Sample order has been cancelled
 			if (form.completeSOChkBox[i].checked == true) {

 				validOrderRequest = true;
 				
 			} else {
 				var partialSampleCancel = false
 				
 				var elem = document.getElementById(form.salesOrder[i] + "partChkBox").elements;
 				
 				// check requested sample parts of the order
				for (j = 0; j < elem.length; j++) {
 					// check if all parts are selected and if so will select whole order
 					if (eval("form." + form.salesOrder[i] + "partChkBox[" + j + "].checked") == true) {
 						 validOrderRequest = true;
 					// partial cancellation
 					} else {
 						partialSampleCancel = true;
 					}
 				 }
 				 if (partialSampleCancel == false) {
 					// show sample order block and select sample order for removal
 					eval(form.salesOrder[i] + "_exp").style.display = "block";
 					form.completeSOChkBox[i].checked = true;
 				 }
 				 
			}
		}
	}

Do I need to try and catch if there's an error when using the length attribute and assume there's only one checkbox for that order or is there a tidier way? Any help is greatly appreciated.

Thanks
 
Apologies, I tidied up the code a bit and left out a hidden field:
Code:
<input type=hidden name="salesOrder" value="<%=Rs("request number")%>">
which should be just after
Code:
<input type=checkbox name="completeSOChkBox" onclick="javascript:selDesel('<%=Rs("request number")%>', this)">
 
I wouldn't use a try/catch - I'd use the information you already know (that "<controlArray>.length" is defined for a group of controls, and undefined for a single control).

So, you could use:

Code:
var checkboxes = form.elements['completeSOChkBox'];
var numOpenRequests = 1;
if (typeof(checkboxes.length) != 'undefined') numOpenRequests = checkboxes.length;

Then you'd need to rework the rest of your code to work for either scenario. There are many ways you could do this, so this is only one option:

Create a local array to loop over instead of using the form elements, as then you could convert your single element into an array and you wouldn't need different code, e.g.:

Code:
var arrayToUse = checkboxes;
if (numOpenRequests == 1)arrayToUse = [checkboxes];

then reference 'arrayToUse' instead of the checkbox elements.

You might find you also need to create an array for the part checkboxes as well... but see how you go.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Cool, I like your thinking. Sounds like a good idea.

I couldn't figure it out so I switched my code so the checkboxes aren't stored as an array. It's not as tidy but hey.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top