1
Good
I have the following code that adds inputs conforms the number of inserted in a text box, however if the form already has some text boxes I just want to add the difference, ie
start text box : 2
After the amendment: amended text box - 3
Right now as I have it’s adding 3 to what already exists,
Follow the code I have at the moment
$(document).ready(function() {
var $txtQuantidade = $('#txtQuantidade');
var $btnAdicionar = $('#btnAdicionar');
var $divForm = $('#divForm');
$btnAdicionar.on('click', function() {
var qtde = $txtQuantidade.val();
console.log(qtde);
var html = '';
for (var i = 0; i < qtde; i++) {
html += '<div>';
html += '<input type="date" id="txtData_' + i + '" name="data[]">';
html += '<input type="time" id="txtHoraIni_' + i + '" name="hinicio[]">'
html += '<input type="time" id="txtHoraFim_' + i + '" name="hfim[]">';
html += '<div>';
}
$divForm.append(html);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtQuantidade" />
<input type="button" id="btnAdicionar" value="Adicionar" />
<div id="divForm"></div>
I’ll try to explain it better:
Let’s imagine that I have 2 in the text box, I can only appear two inputs, if I insert 3 can only show me 3 inputs and finally if I put back 2 could only show me 2 inputs deleting one
thank you
if you are interested, follow your code without jQuery: Jsfiddle
– Tobias Mesquita
Thanks for the help, but that’s not quite it, what controls the number of inputs is the value of the text box, IE, if there are two even if I click it will only keep 2 text boxes, if I insert 3 will just add one more. and if I later write 2 he will only have two, I do not know if I understand :/
– Laranja Mecânica
Okay, I’ve changed the code to what you want: Jsfiddle
– Tobias Mesquita
ey, thank you very much, perfect
– Laranja Mecânica