I have a form where I want to be able to duplicate a group of fields as many times as I want. And I also want to have field id
, name
, and label's for
attributes of those new group of fields increase by 1. I've tried this so far with jQuery, and got it to at least duplicate the group of fields, but remove doesn't work. And I'm not sure how to do +1 for each of those 3 attributes. I appreciate any help I can get.
Here's a jsfiddle of it, http://jsfiddle.net/Unfxn/
HTML
<form method="post" action="#" class="inlineForm" enctype="multipart/form-data">
<div class="repeatingSection">
<a href="#" class="buttonGray buttonRight deleteFight">Delete</a>
<input type="hidden" name="fighter_a_id_1" id="fighter_a_id_1" value="" />
<input type="hidden" name="fighter_b_id_1" id="fighter_b_id_1" value="" />
<input type="hidden" name="winner_id_1" id="winner_id_1" value="" />
<div class="formRow">
<label for="fighter_a_1">Fighters</label>
<input type="text" name="fighter_a_1" id="fighter_a_1" value="" /> <span class="formTextExtraCenter">vs</span> <input type="text" name="fighter_b_1" id="fighter_b_1" value="" />
</div>
<div class="formRow">
<label for="fighter_a_pay_1">Fighter Pay $</label>
<input type="text" name="fighter_a_pay_1" id="fighter_a_pay_1" value="" /> <span class="formTextExtraCenter">vs</span> <input type="text" name="fighter_b_pay_1" id="fighter_b_pay_1" value="" />
</div>
<div class="formRow">
<label for="winner_1">Winner</label>
<input type="text" name="winner_1" id="winner_1" value="" />
</div>
<div class="formRow">
<label for="method_1">Method</label>
<input type="text" name="method_1" id="method_1" value="" />
</div>
<div class="formRow">
<label for="method_type_1">Method Type</label>
<input type="text" name="method_type_1" id="method_type_1" value="" />
</div>
<div class="formRow">
<label for="round_1">Round</label>
<input type="text" name="round_1" id="round_1" class="fieldSmall" value="" />
</div>
<div class="formRow">
<label for="time_1">Time</label>
<input type="text" name="time_1" id="time_1" class="fieldSmall" value="" />
</div>
<div class="formRow">
<label for="fight_number_1">Fight #</label>
<input type="text" name="fight_number_1" id="fight_number_1" class="fieldSmall" value="" />
</div>
</div>
<div class="formRowRepeatingSection">
<a href="#" class="buttonGray buttonRight addFight">Add Fight</a>
</div>
<div class="formRow">
<input type="submit" class="submitButton" value="Save Fights" />
</div>
</form>
JS
// Add a new repeating section
$('.addFight').click(function(){
var lastRepeatingGroup = $('.repeatingSection').last();
lastRepeatingGroup.clone().insertAfter(lastRepeatingGroup);
return false;
});
// Delete a repeating section
$('.deleteFight').click(function(){
$(this).parent('div').remove();
return false;
});
This should automatically rename according all three elements:
// Add a new repeating section
$('.addFight').click(function(){
var currentCount = $('.repeatingSection').length;
var newCount = currentCount+1;
var lastRepeatingGroup = $('.repeatingSection').last();
var newSection = lastRepeatingGroup.clone();
newSection.insertAfter(lastRepeatingGroup);
newSection.find("input").each(function (index, input) {
input.id = input.id.replace("_" + currentCount, "_" + newCount);
input.name = input.name.replace("_" + currentCount, "_" + newCount);
});
newSection.find("label").each(function (index, label) {
var l = $(label);
l.attr('for', l.attr('for').replace("_" + currentCount, "_" + newCount));
});
return false;
});
// Delete a repeating section
$('.deleteFight').live('click',function(){
$(this).parent('div').remove();
return false;
});
I changed the delete handler, to use .live() instead, so that the handler would also be attached to newly created copies of that button. Now, if you're using jquery > 1.7 you should use .on()
The on() version would be:
// Delete a repeating section
$(document).on('click','.deleteFight',function(){
$(this).parent('div').remove();
return false;
});
Use this after you clone your object
Fiddle around :) http://jsfiddle.net/fedmich/MZjKR/1/
I'll leave some TODO for you, change the 3 to the current number of id
<script type="text/javascript">
$.fn.id_changer = function(new_id) {
return this.each(function(){
$(this).find('input').each(function(){
var ob = $(this);
ob.attr('id', this.id.replace(/_\d$/, '_' + new_id));
ob.attr('name', this.name.replace(/_\d$/, '_' + new_id));
});
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With