Closed Thread Icon

Preserved Topic: calling arrays in form elements (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18157" title="Pages that link to Preserved Topic: calling arrays in form elements (Page 1 of 1)" rel="nofollow" >Preserved Topic: calling arrays in form elements <span class="small">(Page 1 of 1)</span>\

 
gaugau
Obsessive-Compulsive (I) Inmate

From: Osterburken, Germany
Insane since: Aug 2001

posted posted 09-04-2001 08:51

what I've got is a JavaScript part in the body of a document where I define form elements, like this:

code:
<form name="myform">
<script language="JavaScript">
for(i = 1; i <= 10; i++)
{
document.writeln("<input type=\"Text\" name=\"foo(" + i + ")\" value=\"\" size=\"10\" maxlength=\"10\" style=\"BORDER:none\" readonly>");
}
</script>
</form>



I want to change all the form elements every time an event occurs by defining a function (in the <head> ) like this:

code:
<script language="JavaScript">
for(i = 1; i <= 10; i++)
{
document.myform.foo[i].value=anothervar[i];
}
</script>



Surprise, it doesn't work! I think this is mainly a question of wrong syntax ("document.myform.foo is not an object");
could anyone tell me the correct syntax for the ...foo... part?

thanks GauGau

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-04-2001 08:58

it surely won't work like that!

you're naming the fields as foo(1), foo(2), foo(3), etc....and you're accessing as foo[i]

to overcome this, just name your fields as: foo (w/o 'i' or any variable), whenever javascript finds two field names with same name then javascript will make it an array!

so your code will become:

for(i = 1; i <= 10; i++){document.writeln("<input type=\"Text\" name=\"foo"\" value=\"\" size=\"10\" maxlength=\"10\" style=\"BORDER:none\" readonly>");}


btw, you can't really name variables as: varname() because the '(' ')' are invalid characters for variable namings.

you can also name instead of foo(1) foo(2) simply foo1 or foo2 or foo3
and then access as:

n = 1;
foon = eval("document.form1.foo"+n);
foon.value = 'hi!';


lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-04-2001 09:00

Oh yes, btw...

Welcome to the Asylum gaugau and enjoy your stay!

3rdperson
Paranoid (IV) Inmate

From: your subconscious. (scared yet?)
Insane since: May 2001

posted posted 09-04-2001 11:40

threep decides to give his head another itch, and asks:
"gangau, why don't you take a step back?
tell us what exactly you want done, and why the form elements need to be written in dynamically. writing stuff in dynamically should be a 'last resort' option.
have you heard of this:
document.myform.elements[x]
that is a predefined property...
so document.myform.elements.length tells you how many elements are in your form, and you can modify them to your heart's content, irrespective of their name.
but, if i know your purpose, i may be able to give you more directions.
so...
you can have:

code:
for(i=1;i<document.myform.elements.length;i++)
etc etc etc


hope this helps, gotta run!"


gaugau
Obsessive-Compulsive (I) Inmate

From: Osterburken, Germany
Insane since: Aug 2001

posted posted 09-04-2001 13:47

thanks to both of you, it works now (who would have doubted ;-) )
Regards GauGau

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-04-2001 19:16

Along the same lines. You pretty much need to name your elements however sometimes this can be problematic using brackets since some languages (PHP) already uses this construction it may conflict with the server side code.

However there is a solution.

JS actually automatically builds a forms array and form elements arrays on page load.

In other words say you have two forms on a page. The first one will be document.forms[0] the second will be document forms[1] etc.

Likewise all the elements are placed in an array so to access the value of the first element of the first form will be
document.forms[0].elements[0].value

piece




:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

« BackwardsOnwards »

Show Forum Drop Down Menu