Dynamically adding rows to an accessible HTML form Given the following HTML form (fragment):

&lt;fieldset id="timesheet-rows"&gt;
    &lt;legend&gt;Add Entries&lt;/legend&gt;

    &lt;div id="timesheetrow-0" class="timesheet-row"&gt;
      &lt;label for="project-0"&gt;Project&lt;/label&gt;
      &lt;select id="project-0" name="project-0" required&gt;
        &lt;option value="" /&gt;
      &lt;/select&gt;

      &lt;label for="department-0"&gt;Department&lt;/label&gt;
      &lt;select id="department-0" name="department-0" required&gt;
        &lt;option value="" /&gt;
      &lt;/select&gt;

      &lt;label for="task-0"&gt;Task: &lt;/label&gt;
      &lt;select id="task-0" name="task-0" required&gt;
        &lt;option value="" /&gt;
      &lt;/select&gt;

      &lt;label for="hours-0"&gt;Hours: &lt;/label&gt;
      &lt;input type="number" step="0.25" id="hours-0" name="hours-0" width="1" placeholder="2.0" required /&gt;

      &lt;label for="comment-0"&gt;Comment: &lt;/label&gt;
      &lt;input type="text" id="comment-0" name="comment-0" width="50" /&gt;
    &lt;/div&gt;

    &lt;input type="button" id="add-row" name="add-row" value="Add row" /&gt;

  &lt;/fieldset&gt;


I have implemented the following jQuery (fragment) to clone each 'timesheet row' &lt;div /&gt; along with the &lt;label /&gt; tags, which are not caught by $(':input'):

$(document).ready(function() {

  var current_id = 0;
  $('#add-row').click(function(){
    next_element($('#timesheetrow-0'));
  })

  function next_element(element){
    var new_element = element.clone(),
        id = current_id + 1;
    current_id = id;
    new_element.attr("id",element.attr("id").split("-")[0]+"-"+id);

    // Ajuster les `id` et `name` dans les &lt;input /&gt;s et &lt;select /&gt;s
    $(':input', new_element).each(function(){
      var field_id = $(this).attr("id"),
          field_name = $(this).attr("name");
      $(this).attr("id", field_id.split("-")[0]+"-"+id );
      $(this).attr("name", field_name.split("-")[0]+"-"+id );
    });

    // Ajuster le for="" dans les &lt;label /&gt;s
    $('label', new_element).each(function(){
      field_for = $(this).attr("for");
      $(this).attr("for", field_for.split("-")[0]+"-"+id );
    });
    new_element.appendTo($("#timesheet-rows"));
  };
});


(Above jQuery was inspired by http://jsfiddle.net/32RgL/ ).

Is there a more elegant or complete way of doing this?
Most of this code is due to the fact that you're increasing the numbers in your attributes. I think that's a mistake. Your rows should all have identical name attributes, stored in an array (e.g. name="task[]").

The only problem would then be the identical IDs. Frankly, you shouldn't be using IDs in the first place. The only advantage of using IDs in this case is to associate the labels to the form fields (so that clicking the label focuses on the form field). This could easily be accomplished by wrapping the label around the form element.

So, to summarize, here are some points to consider:


Get rid of the IDs
Wrap the labels around the form elements to associate them with one another
Get rid of the numbers from the names
Use array like notation (e.g. name="task[]") for your names




Once you've incorporated all this, you can simply clone the row when needed.

HTML:

&lt;fieldset id="timesheet-rows"&gt;
    &lt;legend&gt;Add Entries&lt;/legend&gt;

    &lt;div class="timesheet-row"&gt;

        &lt;label&gt;Project:
            &lt;select name="project[]" required&gt;
                &lt;option value=""&gt;&lt;/option&gt;
            &lt;/select&gt;
        &lt;/label&gt;

        &lt;label&gt;Department:
            &lt;select name="department[]" required&gt;
                &lt;option value=""&gt;&lt;/option&gt;
            &lt;/select&gt;
        &lt;/label&gt;

        &lt;label&gt;Task: 
            &lt;select name="task[]" required&gt;
                &lt;option value=""&gt;&lt;/option&gt;
            &lt;/select&gt;
        &lt;/label&gt;

        &lt;label&gt;Hours:
            &lt;input type="number" step="0.25" name="hours[]" width="1" placeholder="2.0" required /&gt;
        &lt;/label&gt;

        &lt;label&gt;Comment:
            &lt;input type="text" name="comment[]" width="50" /&gt;
        &lt;/label&gt;

    &lt;/div&gt;

    &lt;input type="button" id="add-row" name="add-row" value="Add row" /&gt;
&lt;/fieldset&gt;​


Javascript:

jQuery(function($) {
    var $button = $('#add-row'),
        $row = $('.timesheet-row').clone();

    $button.click(function() {
        $row.clone().insertBefore( $button );
    });
});​


Here's the fiddle: http://jsfiddle.net/wd5y9/