/    Sign up×
Community /Pin to ProfileBookmark

Creating a variable to reference form fields

I am trying to do what I thought would be very simple.
I am trying to create a variable that I will increment to reference a field on a form but I keep getting the following error:”Value is null or not an object”. Here is a code sample:

function timeValidation()
{
recNum = document.control.rCount.value;
i = recNum;
for (x = 1; x <= i; x++)
{
t = “stTime_”;
t = t + x;
alert(t);//t = stTime_1

time = document.control.t;

var h = time.value.indexOf(“:”, 0);

…….so on and so forth.

*The form’s name is ‘control’. I have up to 54 form fields named ‘stTime_1’, ‘stTime_2’ and so on. I am just trying to loop through them by concatenating x to ‘stTime_’ but when I do a document.control.t the value is null. When I do a document.control.stTime_1 I get the form value. I have done alerts and I know that t the first time through the loop t = ‘stTime_1’. Any ideas on how I can make document.control.t reference stTime_1 correctly would be greatly appreciated.
Thanks to anyone that can help!!!!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63May 07.2003 — you are a bit off

it should be something like this

function timeValidation()

{

var frm =document.control;

for (var x = 0; x < i; x++){

if(frm[x].type=="text"){

var field = eval('document.control.'+'stTime_'+x);

alert("field Name = "+field.name+", value = "field.value)

}

}
Copy linkTweet thisAlerts:
@tiffyauthorMay 08.2003 — Khalid-

Thanks for your help!!!!!!
×

Success!

Help @tiffy 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 5.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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...