Click to See Complete Forum and Search --> : Need help with a loop


cweatherford
02-23-2003, 05:27 PM
Hello. I am trying to create a for loop which will append 'i' to the form field selCategory. In the form I create one drop down list for each type of Category in a DB and name each of the fields selCategory1, selCategory2, selCategory3, etc. Now I want to use JavaScript to make sure that the user has selected at least one of the category fields. I am having trouble incrementing the category fields in the JavaScript code though. Here is what I have....

//Loop from one to the number of categories in the DB
for(i=1; i<=NumbCat; i++)
{
Temp = document.AddEvent.selCategory+i+.value
if (Temp == "")
{
//User has not selected this category
x = "false"
}
else
{
//User has selected this category
x = "true"
}
}

I guess that I can not append 'i' to the selCategory field. If anyone has any insite on this please let me know.

I hope this makes sence to everyone. Let me know if you need more info.

Thanks for the help,
Chris :confused:
cweatherford@programnow.com

cweatherford
02-23-2003, 08:28 PM
Great! Thanks for your help! That works like a charm. Here is what I ended up with:

Temp = new Array(NumbCat)

// Loop through all of the Categories and create an array.
// If the array is not blank then the user has selected a category so return true.

for(i=1; i<=NumbCat; i++)
{
if (Cat != "true")
{
Temp = document.AddEvent.elements["selCategory"+i].value
if (Temp != "")
{
//User has not selected this category
Cat = "true"
}
}
}

// Check to see if the user has selected a category
if (Cat != "true")
{
alert("Please select at least one of the categories.")
document.AddEvent.selCategory1.focus()
return false
}

Thanks again for your help.
Chris