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

234
Vistas
Calculate the row date depending on previous row in the table

I have this dynamic table. Add more button append a new row. Row consists of number or days and date field. Means how many days added results in date.

Now in the second row, if I add a number of days; it must check the previous previous row date or number and result the date. But rowSelected.prev('tr')[0] gives me no value.

Can anybody please help me.

$(function() {
  $("#add-more").click(function() {

    $("#main-table").each(function() {
      let tds = '<tr>';
      jQuery.each($('tr:last td', this), function() {
        tds += '<td>' + $(this).html() + '</td>';
      });
      tds += '</tr>';
      if ($('tbody', this).length > 0) {
        $('tbody', this).append(tds);
      } else {
        $(this).append(tds);
      }
    });
  });

  $(document).on('change', '.total-days', function(e) {
    let rowSelected = $(this).closest('tr');


    const someDate = new Date();
    someDate.setDate(someDate.getDate() + parseInt($(this).val()));
    const newDate = someDate.toISOString().substr(0, 10);

    rowSelected.find('.expected-delivery-date').val(newDate);
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="col-sm-6 right">
  <a class="inline btn btn-primary" id="add-more">Add More</a>
</div>


<table class="table table-bordered" id="main-table" border="1">
  <thead>
    <tr>
      <th>No.</th>
      <th>From</td>
        <th>Expected Delivery Date</td>
    </tr>
  </thead>
  <tbody id="rows">
    <tr>
      <td><input class="form-control" type="text" name="deliverableNumber[]" /></td>

      <td><input class="form-control total-days" type="number" value="1" name="deliverableNumberOfDays[]" /></td>

      <td>
        <input class="form-control expected-delivery-date" type="date" name="deliverableExpectedDeliveryDate[]" />
      </td>
      <td><i class="fa-2x fa fa-trash" onclick="SomeDeleteRowFunction(this)" title="Remove row"></i></td>
    </tr>
  </tbody>
</table>

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