I need to add new rows and change form input names of new added rows to a new unique name for example when i click add script will add new rows with input names:
<input type="text" name="11" id="11" />
<input type="text" name="12" id="12" />
<input type="text" name="13" id="13" />
Now I need to change new rows input names to a
<input type="text" name="21" id="21" />
<input type="text" name="22" id="22" />
<input type="text" name="23" id="23" />
I need to add some code at the and of addTableRow function to be able to do that
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function addTableRow(jQtable){
jQtable.each(function(){
var tds = '<tr>';
jQuery.each($('tr:last td', this), function() {tds += '<td>'+$(this).html()+'</td>';});
tds += '</tr>';
if($('tbody', this).length > 0){$('tbody', this).append(tds);
}else {$(this).append(tds);}
});
}
$(function(){
$('table').on('click','tr a',function(e){
e.preventDefault();
$(this).parents('tr').remove();
});
});
</script>
<form id="form1" name="form1" method="post" action="">
<table width="100%" border="1" cellspacing="0" cellpadding="0" id="mans">
<tr>
<td>11
<label for="11"></label>
<input type="text" name="11" id="11" />
</td>
<td>12
<label for="12"></label>
<input type="text" name="12" id="12" />
</td>
<td>13
<label for="13"></label>
<input type="text" name="13" id="13" />
</td>
<td><a onclick="delTableRow($('#mans'));" href="#">del</a></td>
</tr>
</table>
</form>
<button type="button" onclick="addTableRow($('#mans'));">add row</button>
my idea (not working):
var MaxLegnth=1;
function addTableRow(jQtable){
MaxLegnth++;
//alert(MaxLegnth);
jQtable.each(function(){
var tds = '<tr>';
jQuery.each($('tr:last td', this), function() {tds += '<td>'+$(this).html()+'</td>';});
tds += '</tr>';
if($('tbody', this).length > 0){$('tbody', this).append(tds);
}else {$(this).append(tds);}
});
jQuery.each($('tr:last td input', this), function() {$(this).attr('name', 'new_name'); alert($(this).attr('name'));});
}
Here it is a jsFiddle: http://jsfiddle.net/lolipop/yKjuw