I use the following code to add dynamic fields:
$(function() { $(document).ready(function(){ var buttonadd = '<span><button class="btn btn-success btn-add" type="button"><span class="glyphicon glyphicon-plus"></span></button></span>'; var fvrhtmlclone = '<div class="fvrclonned">'+$(".fvrduplicate").html()+buttonadd+'</div>'; $( ".fvrduplicate" ).html(fvrhtmlclone); $( ".fvrduplicate" ).after('<div class="fvrclone form-inline"></div>'); $(document).on('click', '.btn-add', function(e) { e.preventDefault(); $( ".fvrclone" ).append(fvrhtmlclone); $(this).removeClass('btn-add').addClass('btn-remove') .removeClass('btn-success').addClass('btn-danger') .html('<span class="glyphicon glyphicon-minus"></span>'); }).on('click', '.btn-remove', function(e) { $(this).parents('.fvrclonned').remove(); e.preventDefault(); return false; }); }); }); For one form on the page there are no problems. But I can not figure out how to deal with several forms - when adding a field to one of them, the field is inserted into the others. I suppose that when you click the add button, you must pass the id of the parent div block or form to the script, but I can’t figure out how to do it ...
Thank you in advance
PS HTML forms -
<div> <form class="form-horizontal style-form" method="post" id="add_presale_solution_form" name="add_presale_solution_form"> <div class="control-group"> <input type="text" class="form-control" id="name" name="estimate[][name]" value="Вариант 1"> </div> <br> <h4 class="text-left">Смета затрат на подключение</h4> <div class="control-group "> <div class="controls form-inline fvrduplicate"> <label for="name">Наименование</label> <input type="text" class="form-control" id="name" name="estimate[][name]" > <label for="quantity">Количество</label> <input type="text" class="form-control" id="quantity" name="estimate[][quantity]" > <label for="total">Итог</label> <input type="text" class="form-control" id="total" name="estimate[][total]" > </div> </div> <div class="control-group"> <label for="comment">Описание</label> <div class="controls"> <textarea name="comment" id="comment" rows="5" class="form-control" value=""></textarea> </div> </div> <br> <button id="button" type="button" class="btn btn-round btn-primary" data-dismiss="modal">Отправить</button> <button type="button" class="btn btn-round btn-default pull-right" data-dismiss="modal">Отмена</button> </form> </div> <div> <form class="form-horizontal style-form" method="post" id="add_presale_solution_form" name="add_presale_solution_form"> <div class="control-group"> <input type="text" class="form-control" id="name" name="estimate[][name]" value="Вариант 1"> </div> <br> <h4 class="text-left">Смета затрат на подключение</h4> <div class="control-group "> <div class="controls form-inline fvrduplicate"> <label for="name">Наименование</label> <input type="text" class="form-control" id="name" name="estimate[][name]" > <label for="quantity">Количество</label> <input type="text" class="form-control" id="quantity" name="estimate[][quantity]" > <label for="total">Итог</label> <input type="text" class="form-control" id="total" name="estimate[][total]" > </div> </div> <div class="control-group"> <label for="comment">Описание</label> <div class="controls"> <textarea name="comment" id="comment" rows="5" class="form-control" value=""></textarea> </div> </div> <br> <button id="button" type="button" class="btn btn-round btn-primary" data-dismiss="modal">Отправить</button> <button type="button" class="btn btn-round btn-default pull-right" data-dismiss="modal">Отмена</button> </form> </div> Sources were taken from the bootstrap snippet site - http://bootsnipp.com/snippets/XaDoK
class\id- webDev_