0
I have this simple form
<form>
<div class="itenspedido">
<h4>Itens do Pedido</h4>
<div id="itens">
<div class="div-bloco-form">
<label class="obrigatorio">Quantidade:</label>
<input type="text" name="$Quantidade"/>
</div>
</div>
</div>
</form>
here the form will be cloned
<script>
function adicionar() {
var itm = document.getElementById("itens").getElementsByClassName("div-bloco-form")[0];
var cln = itm.cloneNode(true);
var inputs = cln.getElementsByTagName("input");
for (i = 0; i <= inputs.length - 1; i++) {
if (inputs[i].type === "text")
inputs[i].value = "";
}
document.getElementById("itens").appendChild(cln);
}
function remover(btn) {
var div = btn.parentNode;
if (numLinhas() > 1)
div.remove();
}
function numLinhas() {
var linhas = document.getElementById("itens").getElementsByClassName("div-bloco-form");
return linhas.length;
}
</script>
Now the following question, as I will click on the button to clone the form. I would like at the end to record in the database, all Forms. I understand the logic that will be from 1 to N. However the use of is or foreach I believe that this is what I need to use I still don’t know how to do. I am a beginner, I need a help and I thank you from now on.
It is that in the case I wanted to summarize the form so as not to stay a long code. It contains the product name, quantity, unit value and total.
– J.C. Ferri
I got edited my answer by adding code that I could use to change the name of the fields adding numbers to recover in the post within a for with the amount of cloned records.
– Solange
for (inputs[i].name = 0; inputs[i].name + numLines();) { if (inputs[i].type === "text") inputs[i]. value = ""; } would be that?
– J.C. Ferri
for (i = 0; i <= inputs.length - 1; i++) { if (inputs[i].type === "text") inputs[i]. value = "";inputs[i]. name =inputs[i]. name + numinLines(); }
– Solange
is the same for and if that you already have only add in if beyond inputs[i]. value="" also inputs[i]. name = inputs[i]. name + numLine();
– Solange
Thanks for the help @Solange, I’ll try that then.
– J.C. Ferri