"Multiple fields"
Bootstrap 3.1.0 Snippet by zelenin

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.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="container"> <div class="row"> <div class="col-xs-6"> <form> <div class="form-group multiple-form-group" data-max=3> <label>Multiple inputs (with data-max=3)</label> <div class="form-group input-group"> <input type="text" name="multiple[]" class="form-control"> <span class="input-group-btn"><button type="button" class="btn btn-default btn-add">+ </button></span> </div> </div> <div class="form-group multiple-form-group"> <label>Multiple selects</label> <div class="form-group input-group"> <select name="multiple[]" class="form-control"> <option value="">Default select</option> <option value="">Option 1</option> <option value="">Option 2</option> </select> <span class="input-group-btn"><button type="button" class="btn btn-default btn-add">+ </button></span> </div> </div> <div class="form-group multiple-form-group"> <label>Multiple textareas</label> <div class="form-group input-group"> <textarea name="multiple[]" class="form-control" rows="3"></textarea> <span class="input-group-btn"><button type="button" class="btn btn-default btn-add">+ </button></span> </div> </div> </form> </div> </div> </div>
(function ($) { $(function () { var addFormGroup = function (event) { event.preventDefault(); var $formGroup = $(this).closest('.form-group'); var $multipleFormGroup = $formGroup.closest('.multiple-form-group'); var $formGroupClone = $formGroup.clone(); $(this) .toggleClass('btn-default btn-add btn-danger btn-remove') .html('–'); $formGroupClone.find('input').val(''); $formGroupClone.insertAfter($formGroup); var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last'); if ($multipleFormGroup.data('max') <= countFormGroup($multipleFormGroup)) { $lastFormGroupLast.find('.btn-add').attr('disabled', true); } }; var removeFormGroup = function (event) { event.preventDefault(); var $formGroup = $(this).closest('.form-group'); var $multipleFormGroup = $formGroup.closest('.multiple-form-group'); var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last'); if ($multipleFormGroup.data('max') >= countFormGroup($multipleFormGroup)) { $lastFormGroupLast.find('.btn-add').attr('disabled', false); } $formGroup.remove(); }; var countFormGroup = function ($form) { return $form.find('.form-group').length; }; $(document).on('click', '.btn-add', addFormGroup); $(document).on('click', '.btn-remove', removeFormGroup); }); })(jQuery);

Related: See More


Questions / Comments:

Very nice! If i have to add also one dropdown list? i have to modify only the html code? Thx!

Dino () - 10 years ago - Reply 1


Yes, it's cool )
Add demo for select and textarea to snippet.

zelenin () - 10 years ago - Reply 0


Muchas gracias por compartir, me ha sido de mucha utilidad

Daniel Ezquivel () - 7 years ago - Reply 0


How I can add a input[type=file] to this form?

Alex () - 10 years ago - Reply 0


what problem?

zelenin () - 10 years ago - Reply 0


how to set max no's to add field?

Dinesh Chakravarthy () - 10 years ago - Reply 0


i added data-max attribute

zelenin () - 10 years ago - Reply 0