0
I have a simple form with add button that when I click it adds 3 text inputs:
<form method="POST">
<div id="ingredientes">
</div>
<button type="button" id="adicionar">Adicionar</button>
<button type="submit">Cadastrar</button>
</form>
<script>
const adicionar = document.getElementById("adicionar");
const ingredientes = document.getElementById("ingredientes");
adicionar.addEventListener("click", function (event) {
let div = document.createElement("div");
let ing = document.createElement("input");
ing.name = "ingredientes[]";
ing.placeholder = "Ex: Cebola";
ing.type="text";
let qtd = document.createElement("input");
qtd.name = "qtd[]";
qtd.placeholder = "Ex: ¹/² colher de café";
qtd.type="text";
let pre = document.createElement("input");
pre.name = "ingredientes[]";
pre.placeholder = "Ex: Picada";
pre.type="text";
ingredientes.appendChild(ing);
ingredientes.appendChild(qtd);
ingredientes.appendChild(pre);
});
</script>
But now I would like that when adding, the 3 inputs stay inside the Div, should insert so:
<div id="ingredientes">
<div>
<input name="quantidade[]" placeholder="Ex: 1 colher de sopa" type="text">
<input name="ingredientes[]" placeholder="Ex: Cebola" type="text">
<input name="preparo[]" placeholder="Ex: Picada" type="text">
</div>
</div>
opa, thanks, but actually, I think my question was not clear mt, the 3 inputs have to stay inside a div. each 3 inputs are within a div
– Leandro Marzullo
Oops, I’ll make the change in the code
– RickPariz
Take a look now Leandro.
– RickPariz
Just what I needed, thanks!
– Leandro Marzullo