creation removal nested form - Free Download Dynamic Creation And Removal Of Form Fields - nested-form

Free Download Dynamic Creation And Removal Of Form Fields – nested-form

Posted on

This time I will share jQuery Plugin and tutorial about Dynamic Creation And Removal Of Form Fields – nested-form, hope it will help you in programming stack.

creation removal nested form - Free Download Dynamic Creation And Removal Of Form Fields - nested-form
File Size: 15.6 KB
Views Total: 2587
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

Just another jQuery plugin to duplicate form fields that allow the users to dynamically insert more fields into an HTML form when needed.

How to use it:

1. To get started, include the jquery-nested-form.js script after jQuery.

1 <script src="jquery.min.js"></script>
2 <script src="jquery-nested-form.js"></script>

2. Insert an ‘Add’ button to the form group to be duplicated.

01 <div id="container">
02   <div class="nested-form">
03     <p><input type="text" name="basic_attributes[0][text]" id="basic_attributes_0_text"></p>
04     <p><textarea name="basic_attributes[0][textarea]" id="basic_attributes_0_textarea"></textarea></p>
05     <p>
06       <select name="basic_attributes[0][select]" id="basic_attributes_0_select">
07         <option value=""></option>
08         <option value="opt1">opt1</option>
09         <option value="opt2" selected="selected">opt2</option>
10       </select>
11     </p>
12     <p>
13       <input type="hidden" value="" name="basic_attributes[0][radio]">
14       <input type="radio" value="1" name="basic_attributes[0][radio]" id="basic_attributes_0_radio_1">
15       <label for="basic_attributes_0_radio_1">radio1</label>
16       <input type="radio" value="2" name="basic_attributes[0][radio]" id="basic_attributes_0_radio_2" checked="checked">
17       <label for="basic_attributes_0_radio_2">radio2</label>
18     </p>
19     <p>
20       <input type="hidden" value="0" name="basic_attributes[0][checkbox]">
21       <input type="checkbox" value="1" name="basic_attributes[0][checkbox]" id="basic_attributes_0_checkbox" checked="checked">
22       <label for="basic_attributes_0_checkbox">Checkbox</label>
23     </p>
24     <input type="hidden" value="999" name="basic_attributes[0][id]" id="basic_attributes_0_id">
25   </div>
26 </div>
27 <input type="button" value="Add" id="add">

3. Call the plugin on the top container and done.

1 $('#container').nestedForm({
2   forms: '.nested-form',
3   adder: '#add'
4 });

4. It also allows removal of form fields with a ‘Remove’ button.

01 <div id="removable_container">
02   <div class="nested-form">
03     <p>
04       <input type="text" name="removable_attributes[0][text]" id="removable_attributes_0_text">
05     </p>
06   </div>
07   <div class="nested-form">
08     <p>
09       <input type="text" name="removable_attributes[1][text]" id="removable_attributes_1_text">
10       <input type="hidden" name="removable_attributes[1][_destroy]" id="removable_attributes_1__destroy">
11       <input type="button" value="Remove" class="removable-remove">
12     </p>
13   </div>
14 </div>
15 <input type="button" value="Add" id="removable_add">
1 $('#removable_container').nestedForm({
2   forms: '.nested-form',
3   adder: '#removable_add',
4   remover: '.removable-remove'
5 });

5. Customize the start index.

1 <div id="index_container">
2   <div class="nested-form">
3     <p>
4       <input type="text" name="index_attributes[10][text]" id="index_attributes_10_text">
5     </p>
6   </div>
7 </div>
8 <input type="button" value="Add" id="index_add">