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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array of input elements - build name

Status
Not open for further replies.

fohanlon

Programmer
Mar 29, 2011
6
IE
Hi

I have a loop in javascript as follows:

for(i = 0; i < num_rows; i++)
{
document.update.list_i[1] = num.value;
}

What I am trying to do is use the i variable in a loop to build the name of the form element each time for example:

document.update.list_0[1] = num.value;
document.update.list_1[1] = num.value;
document.update.list_2[1] = num.value;

etc. etc.

Could someone help me with the code above i.e. how to change the line of code to know that i is to be 0, 1, 2, 3, 4 etc

Thanks for the help

Fergal.
 
Hi

Thanks for your response.

Tried your code and I am getting an object does not support this property or method. update is the name of my form if this helps.

Regards

Fergal.
 
Hi

There is a loop in the form to create rows in a table (num_rows)

<tr>
<input name="list_<?php echo $count; ?>[]" value="..."></td>
<td valign="top"><input name="list_<?php echo $count; ?>[]" value="..." <?php if($count == 0) echo "onChange=\"change_num(this, '" . $num_rows . "')\""; ?> >

The onChange calls a function called change_num that sets all list_?[1] fields to the value entered in the first list_0[1] field.

Hope you understand this explanation.

Fergal.
 
Sorry I omitted a <td> and a </tr> from above.
 
Hi

When discussing client-side scripting issue please do not post server-side code. That PHP code not really helps. Post the HTML code on which JavaScript operate, the one displayed by the browser's View Source command.

Instead you should post that change_num() function.

In meantime try this :
Code:
[b]for[/b] [teal]([/teal]i [teal]=[/teal] [purple]0[/purple][teal];[/teal] i [teal]<[/teal] num_rows[teal];[/teal] i[teal]++)[/teal] [teal]{[/teal]
  document[teal].[/teal]forms[teal][[/teal][purple]0[/purple][teal]].[/teal]elements[teal][[/teal][green][i]'list_'[/i][/green][teal]+[/teal]i[teal]+[/teal][green][i]'[]'[/i][/green][teal]][[/teal][purple]1[/purple][teal]].[/teal]value [teal]=[/teal] num[teal].[/teal]value[teal];[/teal]
[teal]}[/teal]

Feherke.
 
Thanks so much. This solved my issue. Apologies over the php script. First time poster so I know not to post server side scripting again. Much appreciated. Fergal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top