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

145
Vistas
adding divs dynamically using javascript

I want the user should be able to add 5 divs dynamically on click of plus icon after that alert should come only 5 divs can be added but odd numbers are not coming.

function addvv() {
  var numItems = $('div.box').length;
  if (numItems <= 5) {
    $('.box').clone().appendTo('.container');
    var x = document.getElementById("removediv");
    if (x.style.display === "none") {
      x.style.display = "block";
    }
  } else {
    alert('only 5 can be added');
  }
}

function removediv() {
  $(".box").remove();
}
<div class="container">

  <div class="box">

    <div style="background-color:blue;height: 26px;border-radius: 14px;">

      //this is the button to add divs dynamically

      <i class="fas fa-plus" id="adddiv" onclick="addvv()" style="color: red;float: right;margin-right: 15px;margin-top: 2px;font-size: 18px;"></i>
      <i class="fa fa-trash" id="removediv" onclick="removediv()" style="color: red;float: right;margin-right: 15px;margin-top: 2px;font-size: 18px;display:none"></i>
    </div>
    <div class="row" style="background-color:lightgray;height:100px;border-radius: 23px;">
      <div class="col-md-12" style="padding:10px">
        <div class="col-md-6">
          <label>Name:</label>
          <input type="text" id="name" />
        </div>
        <div class="col-md-6">
          <label>Salary:</label>
          <input type="text" id="salary" />
        </div>
      </div>
    </div>
  </div>
</div>

Please suggest some function so i can add odd number of divs also.Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There were several issues with your code. First, your conditional numLength <= 5 allows running clone() when numLength == 5 which allows a total of 6 clones, not the 5 you intend. Second, $('.box').clone() will clone all elements that match the selector .box. This means on first click it will clone one, on second it will clone two, etc.

I've fixed your code so that it will clone just one, display #removediv on all but the first .box, and remove the .box for which #removediv is clicked. Also, ids should be unique for the whole document so really you should be using classes.

function addvv() {
    var numItems = document.getElementsByClassName('box');
    if (numItems.length < 5) {
        $('.box').first().clone().appendTo('.container');
        $('.box').not(':first').find('#removediv').css('display', 'block');
    }
    else {
        alert('only 5 can be added');
    }
}

function removediv(e) {
    $(e).closest('.box').remove();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <div class="box">
        <div  style="background-color:blue;height: 26px;border-radius: 14px;">
            <!-- //this is the button to add divs dynamically -->
            <i class="fas fa-plus" id="adddiv" onclick="addvv()" style="color: red;float: right;margin-right: 15px;margin-top: 2px;font-size: 18px;">+</i>
            <i class="fa fa-trash" id="removediv" onclick="removediv(this)" style="color: red;float: right;margin-right: 15px;margin-top: 2px;font-size: 18px;display:none">-</i>
        </div>
        <div class="row" style="background-color:lightgray;height:100px;border-radius: 23px;">
            <div class="col-md-12" style="padding:10px">
                <div class="col-md-6">
                    <label>Name:</label>
                    <input type="text" id="name" />
                </div>
                <div class="col-md-6">
                    <label>Salary:</label>
                    <input type="text" id="salary" />
                </div>
            </div>
        </div>
    </div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
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