tags:

views:

52

answers:

3

I have a form, containing a table with rows of items for users to fill in. I currently have it set up so that the user can add an additional row in case they need to add more information.

This works wonderfully, but I'm wondering if there's a way to generate a unique id for each cloned field.

My function is currently this:

 function addTableRow(table) {
         $(table).append($(table + ' tr:last').clone());
         return true;
   }

and is called from an onclick passing in the table id.

The table row contains the following information:

<tr>
     <td><input type="text" id="txtBarnParts1" name="txtBarnParts
 []"></td>
     <td><input type="text" id="txtBarnWidth1" name="txtBarnWidth
 []">ft</td>
     <td><input type="text" id="txtBarnRemarks1"
 name="txtBarnRemarks1[]"></td>
</tr>

What I would like is when I clone the row for the ids to be incremented by 1 so the next row would have ids:

txtBarnParts2, txtBarnWidth2, txtBarnRemarks2...and so on.

Is there a way to do this?

Thanks!

+1  A: 

Try this. Requires jQuery 1.4 or later.

From your usage, I assume table stores an ID with the hash, as in "#myTable".

function addTableRow(table) {
   var $last = $(table + ' tr:last');
   var num = $last.index() + 1; // If the IDs in each row are indexed starting
                                //    with 1, then you would need "+ 2" instead
   $last.clone().find(':input[id]')
         .attr('id', function(i,current) { 
                         return current.replace(/\d+$/, num);
                    })
         .end().appendTo(table);
   return true;
}

This does a .find() on the new (cloned) row, searching for input elements that have an ID attribute. Then it passes a function to the .attr() method, which replaces the current ending digit with the new one.


EDIT: Missed a comma in the .attr() call. Fixed.

EDIT: Had .clone() in the wrong place. Fixed.

patrick dw
can you give the saple code again ??
@user - You mean like a jsFiddle example? Give me just a minute.
patrick dw
@user - Sorry, I had an error. I had `.clone()` in the wrong spot. Fixed. Here's an example. When you add a row, it will popup the HTML so you can see the IDs. http://jsfiddle.net/gWAbk/
patrick dw
tnx.for the quick reply..but i want the row count also appended with the inputs .any ideas?
@user - You mean you want to set the value of the inputs with the number? Easy. Just add `this.value = num;` to the function in `.attr()`. If you want to append it to the current value, do this `this.value = this.value + num;` http://jsfiddle.net/gWAbk/1/
patrick dw
A: 
var $newTr = $(table + ' tr:last').clone();
var newIndex = $newTr.index() + 1;
$newTr.find('[id]').each(function () {
    this.id = this.id.replace(/[0-9]+$/e, newIndex);
});
$(table).append($newTr);
janmoesen
A: 
var i = 1
function addTableRow() {
         $("table").append($("table tr:last").clone());
         $("table tr:last input").attr("name", $("table tr:first input").attr("name")+i);
         i++;
   })

This working with names. Similar would work with id's.

giker
giker - This doesn't erase the old number, so in the case of the `txtBarnParts1` ID, the next would be `txtBarnParts12` then `txtBarnParts123` and so on. :o)
patrick dw
You just have to have in first row id's without numbers.
giker
@giker - But you're cloning the `:last` row, which means you're referencing the previous row that was appended, which has the updated ID. You could change `:last` to `:first`, but it assumes the OP wants a row without numbers.
patrick dw
i don't want the data in the new clonned row .any ideas ?
http://jsfiddle.net/up72J/I'm cloning last, but for index I'm using first row without number + counter
giker
Add $("table tr:last input").val("");on the end
giker
giker - OK, I missed that. Got lost in all the selectors. Still there are 3 different `<input>` elements. Doing `$("table tr:last input").attr("name", $("table tr:first input").attr("name")+i);` would set the `name` attribute of *all* the elements in the `:last` row using the value of the **first** `input` in the `:first` row, so they would end up with the same `name`.
patrick dw