Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
How can I add multiple form controls dynamically when the button is clicked?

When the addIngredientsBtn is clicked, then the program is making a new textbox which is called ingredients in a new line, but I want to make that if I click on this button, then the program is making the quantityList textbox, unit select form and the ingredients textbox, so all of these 3 form in a new line. Here is the code:

sript.js and index.ejs

let addIngredientsBtn = document.getElementById('addIngredientsBtn');
let ingredientList = document.querySelector('.ingredientList');
let ingredeintDiv = document.querySelectorAll('.ingredeintDiv')[0];

addIngredientsBtn.addEventListener('click', function(){
  let newIngredients = ingredeintDiv.cloneNode(true);
  let input = newIngredients.getElementsByTagName('input')[0];
  input.value = '';
  ingredientList.appendChild(newIngredients);
});
<div class="container">
                <div class="col-md-2">
                  <label for="quantityList">Quantity</label>
                  <div class="quantityList">
                    <div class="quantityDiv mb-1">
                      <input type="text" name="quantity" class="form-control">
                    </div>
                  </div>
                </div>

                <div class="col-md-2">
                  <label for="unit">Unit</label>
                  <select class="form-select form-control" name="unit" aria-label="unit">
                    <option selected>select unit</option>
                    <option value="Thai">TSP</option>
                    <option value="American">TBSP</option>
                    <option value="Chinese">CUP</option>
                  </select>
                </div>

                <div class="col-md-8">
                  <label for="ingredientList">Ingredients</label>
                  <div class="ingredientList">
                    <div class="ingredeintDiv mb-1">
                      <input type="text" name="ingredients" class="form-control">
                    </div>
                  </div>
                </div>
          </div>
  
          <div class="col-12">
            <button type="button" class="btn btn-outline-primary" id="addIngredientsBtn">+ Ingredient</button>
          </div>

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda