Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda