473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to refer to a form element?

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
8 1587
"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
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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Don Stefani | last post by:
Hello, I have a form that I want to submit "onchange", OK I've got that working, but when the form submits, I want to pass along a value to a CGI script, as if that value was in a hidden form...
4
by: Steve | last post by:
Hi everyone, I have a test page. When I press the button, it bring form.html, which is fine. When I press Enter key in the textbox, it gives me an error saying this.form.cmd is not an object. ...
3
by: michael | last post by:
let me keep it clean, quick and simple. I am passing a variable into another window and am reassigning the value on the new page - window.document...value = opener.document. ....value and...
5
by: VA | last post by:
Mozilla-based browsers have watch and unwatch methods to detect change in value of form elements (or any Javascript variables really). IE doesn't support this. Is there a a reliable...
15
by: Christoph | last post by:
I'm trying to make it so that if the user sets focus to a form element (text), it automatically sets focus to the very next form element. This will, in effect, prevent the user from modifying the...
14
by: kelvin.jones | last post by:
Hi, if I had the ID of an input element, how can I find the input's FORM in javascript? Basically, given a input's dom id, I want to insert something in the onSubmit of the Form that that input...
12
by: Eric Layman | last post by:
Hi, What's the difference between a normal web element: <input type="text" id="txtname" name="txtname" runat="server"> vs webcontrol text box: <asp:Textbox id="username" Columns="10"...
4
by: rn5a | last post by:
I am storing the physical path of images, along with 3 more columns, in a MS-Access DB table. I want to provide users the option to change this physical image path (as well as the records in the...
1
by: will1 | last post by:
Hi, I have searched for several days to try to resolve my little problem.Sorry but I have very little experience with JavaScript OOP. I have a form which consists of at least one line. Each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.