From: Geoff Cox on
Hello,

I can refer to a form element called value using

f.B1.value (where B1 is the name of the input)

but need to put this into the more general form to cover

f.B2.value, f.B3.value etc

I wrote

var name='B'+test_num;alert(f.name.value);

and this doesn't work.

Also tried

var name='B'+test_num;alert('f.' + name + '.value');

but this just gives 'f.' + name + '.value' and not the actual value.

How do I do this?!

Cheers

Geoff
From: Tom de Neef on
"Geoff Cox" <gcox(a)freeuk.notcom> schreef in bericht
news:9j7j04p8lsid1npsfnri7d5sk18icrhk75(a)4ax.com...
> Hello,
>
> I can refer to a form element called value using
>
> f.B1.value (where B1 is the name of the input)
>
> but need to put this into the more general form to cover
>
> f.B2.value, f.B3.value etc
>
> I wrote
>
> var name='B'+test_num;alert(f.name.value);
>
> and this doesn't work.
>
> Also tried
>
> var name='B'+test_num;alert('f.' + name + '.value');
>
> but this just gives 'f.' + name + '.value' and not the actual value.
>
> How do I do this?!
>

You can access a propertie via f.B2 but also via f[B2]. In the first case B2
needs to be the name of the property; in the second case it needs to be (an
expression that can be evaluated to) a string with the name of the property.
So, experiment with f['B'+test_num].value
Tom


From: Evertjan. on
Tom de Neef wrote on 19 apr 2008 in comp.lang.javascript:

> "Geoff Cox" <gcox(a)freeuk.notcom> schreef in bericht
> news:9j7j04p8lsid1npsfnri7d5sk18icrhk75(a)4ax.com...
>> Hello,
>>
>> I can refer to a form element called value using
>> f.B1.value (where B1 is the name of the input)
>> but need to put this into the more general form to cover
>> f.B2.value, f.B3.value etc
>>
>> I wrote
>> var name='B'+test_num;alert(f.name.value);
>> and this doesn't work.
>> Also tried
>> var name='B'+test_num;alert('f.' + name + '.value');
>> but this just gives 'f.' + name + '.value' and not the actual value.
>> How do I do this?!
>
> You can access a propertie via f.B2 but also via f[B2]. In the first
> case B2 needs to be the name of the property; in the second case it
> needs to be (an expression that can be evaluated to) a string with the
> name of the property. So, experiment with f['B'+test_num].value


Beter use the general approach,
less error prone, more crossbrowser proof:

var n = 2;
var result = document.forms['myForm'].elements['B'+n].value;

It is a good Idea to read the NG FAQ:

<http://jibbering.com/faq/#FAQ4_13>
<http://jibbering.com/faq/#FAQ4_25>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Geoff Cox on
On Sat, 19 Apr 2008 10:37:15 +0200, "Tom de Neef" <tdeneef(a)qolor.nl>
wrote:

>"Geoff Cox" <gcox(a)freeuk.notcom> schreef in bericht
>news:9j7j04p8lsid1npsfnri7d5sk18icrhk75(a)4ax.com...
>> Hello,
>>
>> I can refer to a form element called value using
>>
>> f.B1.value (where B1 is the name of the input)
>>
>> but need to put this into the more general form to cover
>>
>> f.B2.value, f.B3.value etc
>>
>> I wrote
>>
>> var name='B'+test_num;alert(f.name.value);
>>
>> and this doesn't work.
>>
>> Also tried
>>
>> var name='B'+test_num;alert('f.' + name + '.value');
>>
>> but this just gives 'f.' + name + '.value' and not the actual value.
>>
>> How do I do this?!
>>
>
>You can access a propertie via f.B2 but also via f[B2]. In the first case B2
>needs to be the name of the property; in the second case it needs to be (an
>expression that can be evaluated to) a string with the name of the property.
>So, experiment with f['B'+test_num].value
>Tom
>

Many thanks Tom, I was going round in the proverbial circles!

Cheers

Geoff
From: Lasse Reichstein Nielsen on
Geoff Cox <gcox(a)freeuk.notcom> writes:

> I can refer to a form element called value using
>
> f.B1.value (where B1 is the name of the input)

Maybe you can, maybe you can't, but in any case, you shouldn't.
Use the stanard form:
document.forms['f'].elements['B1'].value

> but need to put this into the more general form to cover
>
> f.B2.value, f.B3.value etc
>
> I wrote
>
> var name='B'+test_num;alert(f.name.value);
>
> and this doesn't work.

As it shouldn't. You are loking up a property called "name",
and never referencing the "name" variable.

>
> Also tried
>
> var name='B'+test_num;alert('f.' + name + '.value');

Here you do string manipulation, and the result is a string.

> but this just gives 'f.' + name + '.value' and not the actual value.

It gives the value of the string.

> How do I do this?!

var name = 'B' + test_num;
var value = document.forms['f'].elements[name].value;

/L
--
Lasse Reichstein Nielsen - lrn(a)hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'