Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form elements with the same name

Status
Not open for further replies.

bigS47

Programmer
Jun 29, 2006
20
CA
Hey everyone,

I am currently making a form in which I need to be able to access multiple form objects of the same name.

I was under the impression that if you had 2 form elements with the same name an array would be automatically created so you could then access either of the elements like so:
Code:
<form name="form1" method="post">
<input type="text" value="test" name="samename">
<input type="text" value="test" name="samename">
</form>
Could be accessed with this:
Code:
<script language="javascript">
//to access the first one
document.form1.samename[0].disabled=true;
//to access the second one
alert(document.form1.samename[1].value);
</script>
Unfortunatly, my code does not seem to be working. The firefox javascript console says: "document.form1.samename has no properties."

Anybody have any ideas?
 
try this:

Code:
<script language="javascript">
//to access the first one
document.forms['form1'].elements['samename'][0].disabled = true;
//to access the second one
alert(document.forms['form1'].elements['samename'][0].value);
</script>

if that doesn't work, where are you placing your code? is it before the page loads (in your head tags)? if that's the case, they have no properties because the fields have not yet been rendered by the browser.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
more info:

the page needs to fully load before it knows what objects are. for an example see this:

Code:
<script type="text/javascript"><!--
function doStuff() {
    //to access the first one
    document.forms['form1'].elements['samename'][0].disabled = true;
    //to access the second one
    alert(document.forms['form1'].elements['samename'][0].value);
}

onload = doStuff;
//--></script>



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
cory, wouldn't you want to pass the names of the forms as parameters on the onload call?

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
are you sure? something like this perhaps?

Code:
onload = doStuff("form1", "form2");

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
that'll only work if you have no doctype and put everything in a frameset, verifying that all html occurs outside of your closing </html> tag. i thought you knew that. and besides, the code would be more like this:

Code:
eval( "var i = 1; onload=doS" + "tuff('form' + i + ', fo" + "rm' + (i+1) );" );



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Ahh... that explains why I was having problems....

Why didn't I think to use eval?

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Eh.... not really so much. I've just been answering a lot of questions cause I need my signature to be seen [lol]

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
I'll try that code. The page is loaded though, that part I showed was just a part of a function that is called by an onChange event.
 
ok I tested the code, but it still doesn't work. I have done more tests though and know what the problem is, although I still don't know how to fix it.

The thing is I'm making a training log, and for each "Training" an athlete can do as many "interval" workouts as he wants. I have a drop down menu to ask the athlete how many interval workouts he want's to do, then I use javascript to add rows to a table so the athlete can enter information about each workout. That is why I need the array with the same name thing.

The problem is that the javascript, for some reason, doesn't recognise the dynamically created input. I know this because I created a similar input elsewhere on the page and now it recognises it.

Maybe someone can help now that the problem is put more into perspective.

Other notes:
-The content is changed with the table.insertRow() function, and then with the cell.innerHTML="
HTML:
";
-The input the javascript is trying to get when it causes an error is a menu, but that probably isn't the problem as it has to get other form elements after, it just quits when it gets that error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top