I did some all day learning and I figured out how to add rows dynamically for my form with jquery. But now I can’t for the life of me figure out how to remove that last added row.
$('#btnAdd').click(function() {
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.children('.client').attr('id', 'client' + newNum).attr('name', 'client' + newNum);
newElem.children('.color').attr('id', 'color' + newNum).attr('name', 'color' + newNum);
$('#input' + num).after(newElem);
$('#btnDel').attr('disabled','');
if (newNum == 5)
$('#btnAdd').attr('disabled','disabled');
});
<form id="myForm">
<div id="input1" style="margin-bottom:4px;" class="clonedInput">
<select name="client" id="client" class="client">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="text" name="color" id="color" class="color"/>
</div>
<div>
<input type="button" id="btnAdd" value="Add Row" />
</div>
</form>
And on jsFiddle for your viewing pleasure: Demo
Answer Courtesy of Blender
$('#btnRemove').on('click', function() {
$('.clonedInput').last().remove();
});
Like so?
Demo: http://jsfiddle.net/P8bTz/2/