So what do you do? If you force a value (a zero, for example) for every field, then users who click on a field using a mouse instead of tabbing to the field will have to backspace to make an entry. Further, they might change their minds after backspacing and leave the field empty. If some fields are left empty, then you run into the problem above. However, there's a better way, as this tip will demonstrate.
Javascript has two ways to access a field: by name and by element number. It can loop through every field in a form and work with each name and value it finds, even empty values. You can therefore use javascript to create the comma-separated list you need. This tip simulates receiving items and rows from a database. The assumption is that you had ColdFusion create a list of item names and are expecting javascript to create a corresponding list of quantities from user entries. The form itself consists of a hidden string of item names, rows each containing an item name and a quantity field, and display fields proving a one-to-one correspondence between item names and quantities when the user is done.
<head>
<script language="javascript">
function pack() {
// Reset the display
document.myForm.Quantities.value = '';
var myList
myList = '';
var sep
sep = '';
The for loop walks through every element in the form. If the element's name contains "Qty", then it sets myList equal to the current contents of myList (which starts off empty), the separator (which starts off empty), and the value of the element. It then sets the separator to a comma so that subsequent values will be preceded by a comma.
for (i = 0; i < document.myForm.elements.length; i++) {
if ((document.myForm.elements[i].name.indexOf('Qty') > -1)) {
myList = myList + sep + document.myForm.elements[i].value;
sep = ',';
}
}
The script and page header end with two assignments. The list you've been building gets copied to the Quantities field for display and a hidden list of item names gets copied into the Names field for display.
document.myForm.Quantities.value = myList; document.myForm.Names.value = document.myForm.namelist.value; } </script> </head>
<body> Enter a quantity (or leave it empty) for each item. When you press the evaluate button,<br> the hidden list of names supplied by ColdFusion and the list of quantities supplied by javascript is shown to prove a one-to-one correspondence between them. <p> <form name="myForm" action="fieldlist.cfm" method="get"> <!--- In reality, ColdFusion would generate the following code from a database ---> <input type="hidden" name="namelist" value="Aardvark,Beaver,Cougar,Dachshund"> Aardvark <input type="text" name="Qty" value="" size="4"> Beaver <input type="text" name="Qty" value="" size="4"> Cougar <input type="text" name="Qty" value="" size="4"> Dachshund <input type="text" name="Qty" value="" size="4"><br> <!--- End of "generated" code ---> <input type="button" name="doit" value="Evaluate" onClick="pack();"> <input type="reset" name="nobody" value="Clear All"><p> (List of Names <input type="text" name="Names" value="" size="40">)<br> (List of Quantities <input type="text" name="Quantities" value="" size="40">) </form> </body>