I have this code
<div id="old">
<input type="text" name="id" id="id" size="2" value="1"/>
<input type="text" name="UM" id="UM" size="2"/>
<input type="text" name="DS" id="DS" size="2"/>
</div>
<input type="button" value="Add" id="clone" />
<input type="button" value="remove" id="remove" />
with this Jquery
$(document).ready(function(){
$("input#clone").click(function(){
$("div#old").clone().attr( 'id', 'new_div' ).appendTo("body")
$("#id").val(parseInt($("#id").val()) + 1);
$('#new_div input').each(function() {
$(this).attr('name', $(this).attr('name') + 1);
$(this).attr('id', $(this).attr('id') + 1);
});
});
});
$(document).ready(function(){
$("input#remove").click(function(e) {
if ($('#id').val() == '1')
{
$('remove').prop('disabled', true);
}
else
{
$("#id").val(parseInt($("#id").val()) - 1);
var $el = $('#new_div');
$el.clone(true).appendTo('#new_div');
$el.remove();
}
});
});
But when I delete the cloned element deletes the first one no the last one.
Additional to this I want to autoincrement the name and the id of the inputs in cloned elements like:
- iD1, UM1, DS1
- iD2, UM2, DS2
http://jsfiddle.net/MetCastle/aN37B/
Thanks for all your help !