Getting Started
This guide will help you install and use jQuery Cloner. See deployment for notes on how to deploy this plugin on frontend development on a live website.
Installation
via bower: bower install jquery-cloner
via npm: npm install jquery-cloner
or download or clone (pun!) on GitHub.
Usage
jQuery Cloner relies on classes and attributes to work.
A simple sample markup:
<!-- initialize jQuery-Cloner via the `data-toggle=cloner` attribute -->
<div class="clonable-block" data-toggle="cloner">
<div class="clonable">
<label for="attr_1" class="clonable-increment-for">Attribute <span class="clonable-increment-html">1</span></label>
<input id="attr_1" class="clonable-increment-id clonable-increment-name" type="text" name="attr[0]">
<button type="button" class="clonable-button-close">Delete</button>
</div>
<button class="clonable-button-add" type="button">Add Attributes</button>
</div>
<!-- jQuery is, of course, a dependency -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Our main man here -->
<script src="jquery-cloner/dist/jquery-cloner.min.js"></script>
In the example above, the data-toggle="cloner"
will automatically initialize our clonable-block
.
Manual initialization, then, is as easy as:
// main.js
(function ($) {
$('#my-clonable-block').cloner();
})(jQuery);
Options
The plugin have options you can modify. Below is the list of options with their default values:
$('#my-clonable-block').cloner({
clonableContainer: '.clonable-block', // the selector to contain all clonables
clonable: '.clonable',
addButton: '.clonable-button-add',
closeButton: '.clonable-button-close',
focusableElement: ':input:visible:enabled:first', // this targets all possible input elements
clearValueOnClone: true,
removeNestedClonablesOnClone: true,
limitCloneNumbers: true,
debug: false,
cloneName: 'clonable-clone',
sourceName: 'clonable-source',
clonableCloneNumberDecrement: 'clonable-clone-number-decrement',
incrementName: 'clonable-increment',
decrementName: 'clonable-decrement',
beforeToggle: function (clone, index, self) {},
afterToggle: function (clone, index, self) {},
});
clonableContainer
.clonable-block
The selector that should contain all our clonable elements, including the Add Button.
clonable
.clonable
The selector (usually a class) of the clonable element. This is the html chunk that will be repeated.
addButton
.clonable-button-add
The class of the button that will fire the toggle
method, prompting the cloning action.
closeButton
.clonable-button-close
The class of the button that will fire the remove
method, prompting to remove the clonable element. Important: this element should be inside a clonable
element.
focusableElement
:input:visible:enabled:first
The attribute or input tag inside a newly cloned clonable
to place the cursor over.
clearValueOnClone
true
The plugin will clone the last instance of the clonable
class. This option will toggle to remove or retain all previous input values.
removeNestedClonablesOnClone
true
Option to remove all clones and only retain the original html markup (or the source
).
In the demo below, try to click the `add child` button several times. then click the `add person` button.
Demo | remove all instance of nested cloneName
Children
Hopefully, it removed all clone of source
elements you created when you added another Person.
debug
false
Switch console.log
ging on/off.
incrementName
clonable-increment
This option will be used to increment all values inside a clonable
. It uses suffixes (html, value, and any attribute like class, id, etc.) to know which integers will be incremented.
Sample use case:
<input
id="attr_1"
class="clonable-increment-id clonable-increment-name"
type="text"
name="attr[0]">
In this example, we have the incrementName
of clonable-increment
with suffixes -id
and -name
which corresponds with the input tag's id=attr_1
and name="attr[0]"
. Performing a clone, therefore will result in:
<input id="attr_1" class="clonable-increment-id clonable-increment-name" type="text" name="attr[0]">
<input id="attr_2" class="clonable-increment-id clonable-increment-name" type="text" name="attr[1]">
Notice the cloned input above have an id=attr_2
and name="attr[1]"
, incremented from the cloned html.
Demo | Using incrementation
decrementName
clonable-decrement
The reverse of increment.
Demo | Using decrementation
limitCloneNumbers
true
This option is for limiting the number of clonables
.
Note: as of v1.3.1, this option will only work for decrementing clonables
<div class="clonable-block" data-toggle="cloner">
<div class="clonable clonable-clone-number-decrement" data-clone-number="5">
<label class="clonable-decrement-html clonable-increment-for" for="topfivethings[0]">Thing #5</label>
<input id="topfivethings[0]" type="text" class="form-control clonable-increment-id clonable-increment-name" name="topfivethings[0]">
<button class="btn btn-default clonable-button-close"><i class="fa fa-close"></i></button>
</div>
<button type="button" class="btn btn-default clonable-button-add pull-right"><i class="fa fa-plus"></i> Add Child</button>
</div>
Notice on our clonable
is another class called clonable-clone-number-decrement
, and we've also added an attribute data-clone-number
with the value of 5
.
Demo | limit clonable
s to 5
Top Five Things
beforeToggle
function ($clone, i, self)
This is a function callback you can hook into before the `cloning` action is fired. It accepts parameters $clone
( the clone of the last clonable
), index
(number of clonables
'), and self
(a catch-all reference of the jQuery-Cloner itself).
// html
<div id="beforeToggle" class="clonable-block">
<label for="email_1">Emails</label>
<div class="clonable">
<div class="form-group">
<div class="row">
<div class="col-sm-10">
<input id="email_1" type="email" class="form-control clonable-increment-id clonable-increment-placeholder" placeholder="Email #1">
</div>
<div class="col-sm-2">
<button class="btn btn-default clonable-button-close"><i class="fa fa-close"></i></button>
</div>
</div>
</div>
</div>
<button class="btn btn-success clonable-button-add">Add Email</button>
</div>
// main.js
$('#beforeToggle').cloner({
beforeToggle: function ($clone, i, self) {
// console.log(self);
var $container = self.$container;
if ($clone.find('input:last').val() == "") {
$container.find('input').addClass('error');
} else {
$container.find('input').removeClass('error');
}
},
});
Demo | using beforeToggle
afterToggle
function ($clone, i, self)
This will fire after the cloning
action is triggered.
Demo | using afterToggle
This is a quote generator.
Deployment
Copy the /dist/*.min.js
files to your desired location inside your project
or <script src="jquery-cloner/dist/jquery.cloner.min.js"></script>
after jQuery.
Versioning
The project uses SemVer for versioning. For the versions available, see the tags on this repository.
License
Please see the MIT License.
Acknowledgment
- Andrey Mikhaylov (aka lolmaus) for his jquery.closestchild
- Everyone over at stackoverflow, and other various resources.
- To the Muses of Inspiration