Javascript从动态表单中复制字段

I'm building a website that will allow a user to register many people at one time for something. The user selects the number of people they want to register, and then the next page populates that many forms.

I'm using PHP and CodeIgniter to do this.

For example, lets say there are 3 people being registered, and I am collecting the first name for each person. What I do is loop this 3 times:

<input type="text" name="firstName[]" value="" maxlength="50" class="text small"  />

Notice the name of it.

Well, what I'm trying to do is implement a button that will allow the user to "Copy From Previous". The trouble I'm running into is that I need the name of the previous and current form in order to do it.

On the back when when I save the data, I can access it through index. Is there a way to do this with javascript?

In this case, give them all the same class and loop through the selected elements.

http://jsfiddle.net/Hwdxg/1/

$('input.fields').each(function(){
     $('body').append("<input value='"+$(this).val()+"'>");
});

You can do this using jQuery.

To get an array of input elements with name="firstname[]" you could do:

var firstname_array = $('input[name="firstname[]"]');

You will need to do that every time you add another input with name="firstname[]" to the page.

Then you could access each element through an index like so:

var index = 0;
alert($(firstname_array[index]).val());