"Multiple fields"
Bootstrap 3.1.0 Snippet by zelenin

<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);

Similar snippets: See More


Comments:

comments powered by Disqus