0
Hello, I’m using a script to add input fields to a div:
<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 am working with a ready made template that has several external css’s. So to modify some class properties, I needed to use styles
. When Add Button is clicked 5 times for example, it creates five of these forms one at the bottom of the other. But if I delete the fifth, and create again, it will stop at the sixth position.
I believe the problem is in Tyles, but I don’t know much about this part, can you help me solve this problem? I thank you in advance!
Dude, it worked great, but it’s got a problem with the counter, when you add two and then delete them. if adds again it creates three at once! If I click twice, I would add two courses but are adding four
– Rafa Zanezi
I added
$('.content:last').clone().appendTo(wrapper);
in the selector when cloning, solved! -> http://jsbin.com/fomagifevo/edit?html,css,js,output @Rafazanezi– user3453562
Dude, it worked really cool! I really appreciate it!
– Rafa Zanezi