"Several Dynamic Form Fields on one page - Add & Remove BS3"
Bootstrap 3.3.0 Snippet by ua3acf

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="row"> <div class="col col-md-2"> <div class="fld_wrap" id="fld1"> <div class="input-group"> <input class="form-control" type="text" name="fld_val1"> <span class="input-group-btn"><button fldnum="1" class="btn btn-success add_button" type="button"><span class="glyphicon glyphicon-plus"></span></button></span> </div> </div> Click + to add another form field </div> <div class="col col-md-2"> <div class="fld_wrap" id="fld2"> <div class="input-group"> <input class="form-control" type="text" name="fld_val2"> <span class="input-group-btn"><button fldnum="2" class="btn btn-success add_button" type="button"><span class="glyphicon glyphicon-plus"></span></button></span> </div> </div> Click + to add another form field </div> <div class="col col-md-2"> <div class="fld_wrap" id="fld3"> <div class="input-group"> <input class="form-control" type="text" name="fld_val3"> <span class="input-group-btn"><button fldnum="3" class="btn btn-success add_button" type="button"><span class="glyphicon glyphicon-plus"></span></button></span> </div> </div> Click + to add another form field </div> </div>
.input-group:not(:first-of-type) { margin-top: 10px; } .glyphicon { font-size: 12px; }
$(document).ready(function(){ $('.add_button').click(function(){ var kakoi=$(this).attr('fldnum'); var insHTML = '<div class="input-group"><input class="form-control" type="text" name="fld_val'+kakoi+'"><span class="input-group-btn"><button class="btn btn-danger remove_button" type="button"><span class="glyphicon glyphicon-minus"></span></button></span></div>'; $("#fld"+kakoi).append(insHTML); }); $('.fld_wrap').on('click', '.remove_button', function(e){ e.preventDefault(); $(this).parents(':eq(1)').remove(); }); });

Related: See More


Questions / Comments: