need help with the FOR i=0.. case

N

namemattersnot

Re,

I have the following vars:

resp.date1 = "date1"';
resp.date2 = "date2";
resp.date3 = "date3";
resp.date4 = "date4";

I do a simple for " (i=1;i<4;i++) { alert('resp.date'+i); } " but it
alerts with "resp.dateNUMBER", instead of the var's value.

How do I accomplish this?

Thanks!
 
N

namemattersnot

someone suggested the following solution that works:

b = 'a = resp.date'+i;
eval(b);
alert(a);

:)
 
N

namemattersnot

Vivek, it will not help becase it will try to ADD "i", instead of
appending it.

I found the working solution. Thanks for the answer though.
 
R

RobG

Re,

I have the following vars:

resp.date1 = "date1"';
resp.date2 = "date2";
resp.date3 = "date3";
resp.date4 = "date4";

I do a simple for " (i=1;i<4;i++) { alert('resp.date'+i); } " but it

Hopefully you are declaring 'i' as a local variable:

for (var i=1; i<4; i++) ...

alerts with "resp.dateNUMBER", instead of the var's value.

How do I accomplish this?

Use:

alert( resp['date' + i] );


Using strings to access object properties is covered in the group FAQ:

<URL:http://www.jibbering.com/faq/#FAQ4_39>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top