I have multiple groups of dynamic fields I need to add to a form, I used an example to get the main bit working, but I need to setup the javascript to 'target' specific set of
You can see that when the add button is clicked it adds[clones] a new field in the file-controls div. I need to somehow detect the class of the parent [well, 4th predecessor] div when the add button gets clicked.
How is this done?
HTML:
<form id="form" name="form" method="post" class="form" role="form" action="[[~[[*id]]]]" enctype="multipart/form-data">
<input type="hidden" name="id" value="[[+id]]">
<div class="row">
<div class="form-group col-sm-6 [[!+fi.error.expires:notempty=` has-error`]]">
<label for="expires">Expires</label>
<select name="expires" class="form-control">
<option value="24">24 hours</option>
<option value="48">48 hours</option>
<option value="72">72 hours</option>
<option value="96">96 hours</option>
</select>
</div>
<div class="form-group col-sm-6 [[!+fi.error.origin:notempty=` has-error`]]">
<label for="origin">Origin</label>
<input name="origin" type="text" id="origin" value="" class="form-control">
</div>
</div>
<div class="row">
<div class="col-sm-12"><label>Tag numbers: </label></div>
<div class="tag-controls">
<div class="entry col-sm-4">
<div class="input-group">
<input class="form-control" name="fields[]" type="text" placeholder="Type something">
<div class="input-group-btn">
<button class="btn btn-success btn-add" type="button"><span class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12"><label>Photos: </label></div>
<div class="image-controls">
<div class="entry col-sm-4">
<div class="input-group">
<input class="form-control" name="fields[]" type="text" placeholder="Type something">
<div class="input-group-btn">
<button class="btn btn-success btn-add" type="button"><span class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12"><label>Files: </label></div>
<div class="file-controls">
<div class="entry col-sm-4">
<div class="input-group">
<input class="form-control" name="fields[]" type="text" placeholder="Type something">
<div class="input-group-btn">
<button class="btn btn-success btn-add" type="button"><span class="glyphicon glyphicon-plus"></span></button>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" name="submit" id="submit" value="submit" class="btn button-sm btn-primary">Add Special Offering</button>
</div>
</form>
Javascript:
// dynamic add form fields
$(function(){
$(document).on('click', '.btn-add', function(e){
console.log('button clicked');
e.preventDefault();
var controlForm = $('.file-controls'),
currentEntry = $(this).parents('.entry:first'),
newEntry = $(currentEntry.clone()).appendTo(controlForm);
console.log(controlForm);
console.log(currentEntry);
console.log(newEntry);
newEntry.find('input').val('');
controlForm.find('.entry:not(:last) .btn-add')
.removeClass('btn-add').addClass('btn-remove')
.removeClass('btn-success').addClass('btn-info')
.html('<span class="glyphicon glyphicon-minus"></span>');
}).on('click', '.btn-remove', function(e)
{
$(this).parents('.entry:first').remove();
e.preventDefault();
return false;
});
});
$(this).parents('.entry:first');to$(this).parents('.image-controls');