<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
// Adicionar curso
$(document).ready(function(){
var maxField = 15; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div style="position:relative; height:100px;"><div><div class="form-group" style="position:absolute;top:-50px;"><div class="field_wrapper"><div class="form-group" style="width:470px; position:absolute;"><label>Instituição</label><input type="text" class="form-control" name="field_name[]" value="" placeholder="Nome e sede"/></div><div class="form-group" style="width:300px; position:absolute; left:510px;"><label>Curso</label><input type="text" class="form-control" name="field_name[]" value=""/></div><div class="form-group" style="width:170px; position:relative; left:790px;"><label>Conclusão</label><select class="form-control"><option></option><option>Concluido em:</option><option>Espero concluir em:</option></select></div><input class="form-control" placeholder="Ano" style="width:75px; position:relative; left:970px; top:-49px;" name="field_name[]" value=""/><a href="javascript:void(0);" class="remove_button" style="position:absolute; top:60px;" title="Remove field">Remover Curso</a></div></div>'; //New input field html
var contCurso = 1; //Initial field counter is 1
$(addButton).click(function(){ //Once add button is clicked
if(contCurso < maxField){ //Check maximum number of input fields
contCurso++; //Increment field counter
$(wrapper).append(fieldHTML); // Add field html
}
});
$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
contCurso--; //Decrement field counter
});
});
and the initial form:
<a href="javascript:void(0);" class="add_button" title="Add field">Adicionar Cursos</a>
<div>
<div class="form-group" style="width:470px; position:absolute;">
<label>Instituição</label>
<input type="text" class="form-control" name="field_name[]" value="" placeholder="Nome e sede"/>
</div>
<div class="form-group" style="width:300px; position:absolute; left:510px;">
<label>Curso</label>
<input type="text" class="form-control" name="field_name[]" value=""/>
</div>
<div class="form-group" style="width:170px; position:relative; left:790px;">
<label>Conclusão</label>
<select class="form-control">
<option></option>
<option>Concluido em:</option>
<option>Espero concluir em:</option>
</select>
</div>
<input class="form-control" placeholder="Ano" style="width:75px; position:relative; left:970px; top:-49px;" name="field_name[]" value=""/>
</div>
The script works, but I have a visual problem on the site. I'm working with a ready template that has several external css's. So to modify some properties of the class, I needed to use styles
. When Add Button is clicked 5 times for example, it creates five of these forms one underneath the other. But if I exclude the fifth, and re-create, it will stop at the position of the sixth.
I believe the problem is in the styles, but I do not know much about this part, can you help me solve this problem? Thank you in advance!