Forum Moderators: open

Message Too Old, No Replies

Dynamic text field value

         

smagdy

10:08 pm on Nov 28, 2004 (gmt 0)

10+ Year Member



Well ive this in Javascript tag in head

for(var i=0; i<a; i++)
{
text="fname"+i;

full=document.fo.text.value;

}

So it give the famous error:
document.fo.text.value(null or has no value) so is there specia concatenation for the "text" cuz its real value is fname0.

btw i checked viewSource and the field name actually name is fname0

thanks in advance

MichaelBluejay

2:05 am on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm having a hard time understanding your English. I'm also not sure what the end result you're looking for is.

for(var i=0; i<a; i++)

What this means is:

1. Set the value of the variable "i" to zero.
2. As long as "i" is less than "a" do the block below.
3. Add 1 to the value of "i".

But you've never defined "a", so "a" is effectively equal to zero. So "i" will *never* be less than "a". So the code block will never execute.

text="fname"+i;

This produces a string such as "fname0" or "fname1". It doesn't get any value from a form field, if that's what you were trying to do. And it won't actually run anyway as mentioned above.

full=document.fo.text.value;

This sets the value of the variable "full" to the value of the field "text" in the form called "fo". But it won't actually run either, as mentioned above.

Actually, if you're getting the error message you mentioned, then maybe you defined "a" elsewhere so your IF block actually runs, but since you posted incomplete code I can't really see what's going on.

Anyway, the way to reference the value of a field is:

document.form.field.value

Replace "form" with the name of the form, and "field" with the name of the field. Or, you could name your form "form" and your field "field".

Hope this helps.

smagdy

9:51 am on Nov 29, 2004 (gmt 0)

10+ Year Member



Well am sorry for being unclear.. i was very sleepy.
so about the "a" it has a value but I just shorten the code to not make it complicated.

so now in this example:
document.form.field.value

my field name is dynamic so its equal fname0, fname1 and so on.

my question is.. whats wrong in this?

i=0;
field=="fname"+i;
full=document.form.field.value; //I get error here Null or no value.

smagdy

10:00 am on Nov 29, 2004 (gmt 0)

10+ Year Member



Sorry there was typoing mistake in the last code.

i=0;
field="fname"+i;
full=document.form.field.value; //I get error here (document.form.field.value is null or not an object)

dcrombie

10:48 am on Nov 29, 2004 (gmt 0)



I think you're looking for something like:

full = document.form["fname" + i].value;

smagdy

11:04 am on Nov 29, 2004 (gmt 0)

10+ Year Member



Yess thats it .. it worked but can u explain why it has to be like that?

MichaelBluejay

11:52 am on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's just Javascript syntax. You have to give Javascript a way to know when you're talking about a variable vs. when you're talking abou static text. When you refer to a form Javascript figures you're typing the actual name of the form and not a variable, unless you use the brackets.