/    Sign up×
Community /Pin to ProfileBookmark

Trying to reference a textbox array

I’m having some trouble with Javascript and Coldfusion.

I have 10 dynamically created textboxes and select boxes on my page. eg:
<cfoutput>
<cfloop from=”1″ to=”10″ index=”Variables.myLoop”>
<input type=”text” name=”textBox#myLoop#”>
….
<select name=”selectbox#myLoop#”>

Now I need to do validation on this but I cannot get Javascript to reference the name in a loop

for (foo = 1; foo < 11; foo++) {
if (document.form.textbox[foo].value != “”)
…..

My error occurs in the above line. Can anybody help or point me in the right direction?
Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@violent_jApr 26.2004 — try something like this:

for (foo = 1; foo < 11; foo++) {

name = 'textbox' + foo;

if (document.form.textbox[name].value != "")

grtz
Copy linkTweet thisAlerts:
@wyattfauthorApr 26.2004 — Tried that. I get an "Expected identifier" error. I know the solution is something simple I just can't see it
Copy linkTweet thisAlerts:
@violent_jApr 26.2004 — try:

for (foo = 1; foo < 11; foo++) {

var name = 'textbox' + foo;

if (document.form.textbox[name].value != "")

grtz
Copy linkTweet thisAlerts:
@wyattfauthorApr 26.2004 — got it working with

for (foo= 1; foo < 11; foo++) {

var boxname= 'textbox'+ foo;

alert(document.form.elements[boxname].value);

}

Thanks for the help j you got me thinking the right way.
×

Success!

Help @wyattf 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.17,
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,
)...