By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

how to refer to a form element?

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Geoff Cox" <gc**@freeuk.notcomschreef in bericht
news:9j********************************@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
Jun 27 '08 #2

P: n/a
Tom de Neef wrote on 19 apr 2008 in comp.lang.javascript:
"Geoff Cox" <gc**@freeuk.notcomschreef in bericht
news:9j********************************@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)
Jun 27 '08 #3

P: n/a
On Sat, 19 Apr 2008 10:37:15 +0200, "Tom de Neef" <td*****@qolor.nl>
wrote:
>"Geoff Cox" <gc**@freeuk.notcomschreef in bericht
news:9j********************************@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
Jun 27 '08 #4

P: n/a
Geoff Cox <gc**@freeuk.notcomwrites:
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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 27 '08 #5

P: n/a
On 19 Apr 2008 08:45:53 GMT, "Evertjan."
<ex**************@interxnl.netwrote:
>var n = 2;
var result = document.forms['myForm'].elements['B'+n].value;

It is a good Idea to read the NG FAQ:
OK thanks Evertjan

Cheers

Geoff
>
<http://jibbering.com/faq/#FAQ4_13>
<http://jibbering.com/faq/#FAQ4_25>
Jun 27 '08 #6

P: n/a
On Sat, 19 Apr 2008 11:09:37 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.comwrote:

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

Cheers

Geoff
Jun 27 '08 #7

P: n/a

Hi!
One option is iterating over the collection of elements in the form.
An example:

for(var i=0;i<form.elements.length;i++){
var nameElement=form.elements[i].name;
var valueElement=form.elements[i].value;

// do something...
}

I hope this can help you.

Felipe Silva.
Chile.

On Apr 19, 3:32*am, Geoff Cox <g...@freeuk.notcomwrote:
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
Jun 27 '08 #8

P: n/a
On Sun, 20 Apr 2008 11:41:10 -0700 (PDT), fssm2666
<fs******@gmail.comwrote:
>
Hi!
One option is iterating over the collection of elements in the form.
An example:

for(var i=0;i<form.elements.length;i++){
var nameElement=form.elements[i].name;
var valueElement=form.elements[i].value;

// do something...
}

I hope this can help you.

Felipe Silva.
Thanks Felipe - will try that out.

Cheers

Geoff
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.