form cloner - Download Add/Remove Form Fields Dynamically With jQuery - cloner

Download Add/Remove Form Fields Dynamically With jQuery – cloner

Posted on

This time I will share jQuery Plugin and tutorial about Add/Remove Form Fields Dynamically With jQuery – cloner, hope it will help you in programming stack.

form cloner - Download Add/Remove Form Fields Dynamically With jQuery - cloner
File Size: 65.2 KB
Views Total: 2850
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A lightweight, flexible form cloner plugin which enables the user to dynamically add/remove/clone form fields and/or form groups in an HTML form.

How to use it:

1. Install the jQuery cloner via NPM or directly download the plugin and then include the JavaScript file jquery.cloner.min.js after jQuery library.

1 # NPM
2 $ npm install jquery-cloner --save
1 <script src="/path/to/cdn/jquery.slim.min.js"></script>
2 <script src="dist/jquery.cloner.min.js"></script>

2. Initialize the plugin by adding the data-toggle=”cloner” attribute to the clonable form block.

1 <div class="clonable-block" data-toggle="cloner">
2   ...
3 </div>

3. Insert any form fields to be clonable to the form block. That’s it. Possible CSS helpers:

  • clonable: CSS selector for the clonable form field
  • clonable-decrement-*: decrements values inside the form field
  • clonable-increment-*: increments values inside the form field
  • clonable-button-add: CSS selector for the Add button
1 <div class="clonable-block" data-toggle="cloner">
2   <div class="clonable" data-ss="1">
3     <label for="" class="clonable-increment-html">1 2</label>
4     <input id="person_1" class="clonable-decrement-value clonable-increment-placeholder" type="text" name="person[0]" placeholder="person[0][name][1]" value="5 4">
5   </div>
6   <button class="clonable-button-add" type="button">Add person</button>
7 </div>

4. You can also initialize the plugin via JavaScript:

1 $(function(){
2   $('#clonable-block').cloner();
3 });

5. All possible plugin options:

01 $('#clonable-block').cloner({
02   clonableContainer: '.clonable-block',
03   clonable: '.clonable',
04   addButton: '.clonable-button-add',
05   closeButton: '.clonable-button-close',
06   focusableElement: ':input:visible:enabled:first',
07  
08   clearValueOnClone: true,
09   removeNestedClonablesOnClone: true,
10   limitCloneNumbers: true,
11  
12   debug: false,
13  
14   cloneName: 'clonable-clone',
15   sourceName: 'clonable-source',
16  
17   clonableCloneNumberDecrement: 'clonable-clone-number-decrement',
18  
19   incrementName: 'clonable-increment',
20   decrementName: 'clonable-decrement',
21 });

6. Or pass the options via data attribute:

1 <div class="clonable-block"
2      data-toggle="cloner"
3      data-options='{"clearValueOnClone":false}'>
4   ...
5 </div>

7. Callback functions which will be fired before/after cloning.

1 $('#clonable-block').cloner({
2   beforeToggle: function (clone, index, self) {},
3   afterToggle: function (clone, index, self) {}
4 });

This awesome jQuery plugin is developed by lienoil. For more Advanced Usages, please check the demo page or visit the official website.

source : jquery.net