Sample code snippet for Adding and Removing the Elements dynamically using jQuery
{code type=html}
{/code}
Javascript
{code type=html}
$(document).ready(function() {
$(“#addchoice”).click(function(){
//getting the current element count
var current = $(“#answercount”).val();
current = Number(++current);
//Append the html content
$(“#choiceTemplate”).append(”
“);
//assign the current element count
$(“#answercount”).val(current);
return false;
});
//remove the elements
$(“#removechoice”).click(function(){
//getting the current/last element count
var current = $(“#answercount”).val();
//removing the element
$(‘#current’+current).remove();
//assign the current element count
current = Number(–current);
$(“#answercount”).val(current);
});
});
{/code}
Submitted By : Raja



Hi Raja,
Thanks for the post, What plugins we require to run the above mentioned code?
Thanks
Harry
Hi Harry,
No plugin needed..this is simple jquery code.
any issue with the above code? i am happy to help you.
Thanks,
Raja.
one error on .append() function
Please check the demo and see view source of demo http://ajaxmint.com/demo/add-remove-elements.html
the below line
$("#choiceTemplate").append("");
should be
$("#choiceTemplate").append("Answer Choice " + current + " :
");
its really demanding.
thanks for this code
Hi,
i would like to place the remove button near each input field, so that the user can remove a specific text field…
can u plz help me ?