/    Sign up×
Community /Pin to ProfileBookmark

Need help with a loop

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 ?
[email][email protected][/email]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@cweatherfordauthorFeb 24.2003 — 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
×

Success!

Help @cweatherford spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...