Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cballou/414140f734fca789a2fe841dd4f0bcdb to your computer and use it in GitHub Desktop.
Save cballou/414140f734fca789a2fe841dd4f0bcdb to your computer and use it in GitHub Desktop.
Example usage of the jQuery Form Element Repeater Plugin
// default values
$.fn.repeater.defaults = {
groupClass: 'r-group',
btnAddClass: 'r-btnAdd',
btnRemoveClass: 'r-btnRemove',
minItems: 1,
maxItems: 0,
startingIndex: 0,
reindexOnDelete: true,
repeatMode: 'insertAfterLast', // append, prepend, insertAfterLast
animation: null,
animationSpeed: 400,
animationEasing: 'swing',
clearValues: true,
beforeAdd: function($doppleganger) { return $doppleganger; },
afterAdd: function($doppleganger) { },
beforeDelete: function() { },
afterDelete: function() { }
};
<div class="container">
<div class="r-group">
<p>
<label for="vehicle_0_name" data-pattern-text="Vehicle Name ++:">Vehicle Name 1:</label>
<input type="text" name="vehicle[0][name]" id="vehicle_0_name" data-pattern-name="vehicle[++][name]" data-pattern-id="vehicle_++_name" />
</p>
<p>
<label for="vehicle_0_type" data-pattern-text="Vehicle Type ++:">Vehicle Type 1:</label>
<input type="text" name="vehicle[0][type]" id="vehicle_0_type" data-pattern-name="vehicle[++][type]" data-pattern-id="vehicle_++_type" />
</p>
<p>
<!-- Manually a remove button for the item. -->
<!-- If one didn't exist, it would be added to overall group -->
<button type="button" class="r-btnRemove">Remove -</button>
</p>
</div>
<!-- The add button -->
<button type="button" class="r-btnAdd">Add +</button>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="jquery.form-repeater.js"></script>
<script>
$('.container').repeater({
btnAddClass: 'r-btnAdd',
btnRemoveClass: 'r-btnRemove',
groupClass: 'r-group',
minItems: 1,
maxItems: 0,
startingIndex: 0,
reindexOnDelete: true,
repeatMode: 'append',
animation: null,
animationSpeed: 400,
animationEasing: 'swing',
clearValues: true
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment