Skip to content Skip to sidebar Skip to footer

Copy Table Row And Increment All Name Attributes

Let's have a table like this:

Solution 1:

$("#newRowButton").click(function() {
  $("table tr:last")
      .clone()
      .appendTo("table")
      .find(':input')
      .attr('name', function(index, name) {
          return name.replace(/(\d+)$/, function(fullMatch, n) {
              returnNumber(n) + 1;
          });
      })
});

Live demo.

Solution 2:

$("#newRowButton").click(function(){
   var trows = $("table tr:last").clone();
   trows.find('td input').attr("name",function(i,a){
   var p = newRegExp("((?:[a-z][a-z]+))(\\d+)",["i"]);
   var m = p.exec(a);
   var index = parseInt(m[1]);
   index++;
   return m[0]+index;
   });
   trows.appendTo("table");
});

Solution 3:

var clonedRow = $("#EventType tr:last").clone();
$("input", clonedRow).attr('name', 'FirstName3'); // reset the name

you can get the name attribute and increment by 1

Reference

Post a Comment for "Copy Table Row And Increment All Name Attributes"