I intend to add dynamic inputs if necessary, in a form that is also returned dynamically in js. I am trying to do it this way, but it is not adding new inputs if necessary.
Code:
for (var i = 0; i < data1.length; i++) {
Id = data1[i][0];
DataRegisto = data1[i][1];
Cliente = data1[i][2];
Orcamento = data1[i][3];
Ref = data1[i][4];
Designacao = data1[i][5];
Quantidade = data1[i][6];
Valor = data1[i][7];
linha1 += `<div class="input_fields_wrap1">
<div class="form-group col-md-2">
<input type="text" class="form-control1" name="Refe[]" value="${Ref}">
<span class="form-highlight"></span>
<span class="form-bar"></span>
<label class="label1" for="Refe">Refª</label>
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control1" name="Valo[]" value="${Valor}">
<span class="form-highlight">$</span>
<span class="form-bar"></span>
<label class="label1" for="Valo">Valor</label>
</div>
<button class="btn btn-warning caixa add_field_button1"><i class="fa fa-plus-square fa-5x taman" aria-hidden="true"></i></button>
</div>`;
$("#retorc1").html(linha1);
$(document).ready(function() {
var max_fields1 = 100; //maximum input boxes allowed
var wrapper1 = $(".input_fields_wrap1"); //Fields wrapper
var add_button1 = $(".add_field_button1"); //Add button ID
var x1 = 1; //initlal text box count
$(add_button1).click(function(e) { //on add input button click
e.preventDefault();
var length1 = wrapper1.find("input:text").length;
if (x1 < max_fields1) { //max input box allowed
x1++; //text box increment
$(wrapper1).append('<div id="teste1"><div class="form-group col-md-2"><input type="text" class="form-control1" name="Refe[]" /><span class="form-highlight"></span><span class="form-bar"></span><label class="label1" for="Refe">Refª</label></div><div class="form-group col-md-6"><input type="text" class="form-control1" name="Designaca[]" /><span class="form-highlight"></span><span class="form-bar"></span><label class="label1" for="Designaca">Designação</label></div><div class="form-group col-md-2"><input type="text" class="form-control1" name="Qtda[]" /><span class="form-highlight"></span><span class="form-bar"></span><label class="label1" for="Qtda">Quantidade</label></div><div class="form-group col-md-2"><input type="text" class="form-control1 Preco1" name="Valo[]" value="0.00" /><span class="form-highlight">$</span><span class="form-bar"></span><label class="label1" for="Valo">Valor</label></div><button class="remove_field1" style="background-color: #313348;"><span class="fa fa-trash fa-fw" aria-hidden="true"></span></button></div>');
}
$('.Preco1').maskMoney({ decimal: '.', thousands: ' ', precision: 2 });
});
$(wrapper1).on("click", ".remove_field1", function(e) { //user click on remove text
e.preventDefault();
$("#teste1").remove();
x1--;
})
});
No error is returning, but when I click the button to add lines, it does nothing
I'm trying this way:
I added the onclick to the button:
<button class="btn btn-warning caixa" onclick="adcElemento();"><i class="fa fa-plus-square fa-5x taman" aria-hidden="true"></i></button>
Then I created the following function
function adcElemento() {
let myForm;
myForm = document.getElementById('retorc1');
document.body.appendChild(myForm);
let myInput;
myInput = document.createElement('input');
myForm.appendChild(myInput);
myInput.setAttribute('type', 'text');
myInput.setAttribute('name', 'Refe[]');
}
But it does not add inputs to the form.