1

I have a multiple input rows and all rows have a total column at the end. Now, I want to put the total result of each rows in column Total. I tried but it returns NaN value instead of total result. Attached is my view & script please help what I am missing??? Thanks

$('body').on('change', '.bgpending, .registered', function() {
  var el = $(this);
  var totalRow = 0;
  el.closest('.dosiaPlace').find('input').each(function() {
    totalRow = totalRow + parseInt($(this).val());
  });
  el.closest('.dosiaPlace').find('.total-row').val(totalRow);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dosiaPlace">
  <div class="col-md-2">
    <label>@lang('dashboard.pastyear'):</label>
    <input type="number" value="0" class="form-control bgpending" name="bgpending[]" />
  </div>
  <div class="col-md-2">
    <label>@lang('dashboard.warida'):</label>
    <input type="number" value="0" class="form-control registered" name="registered[]" />
  </div>
  <div class="col-md-2">
    <label>@lang('dashboard.total'):</label>
    <input type="text" class="form-control total-row" />
  </div>
</div>

Result:

JS code result

1
  • can you check logs of this value $(this).val() and make sure you are getting it right? so that hou can see what parseInt is getting? Commented Jan 4, 2020 at 8:33

2 Answers 2

3

The container, the .dosiaPlace, I presume, has 3 inputs: the .bgpending, the .registered, and the .total-row. Your el.closest('.dosiaPlace').find('input') finds all 3 of those, including the .total-row. But the .total-row is empty (initially), so

totalRow = totalRow + parseInt($(this).val());

for it results in

totalRow = totalRow + parseInt('');

And the empty string can't be parseInt'd (results in NaN). (You also probably don't want to be including the .total-row anyway)

Use input:not(.total-row') instead of input:

$('body').on('change', '.bgpending, .registered', function() {
  var el = $(this);
  var totalRow = 0;
  el.closest('body').find('input:not(.total-row)').each(function() {
    totalRow = totalRow + parseInt($(this).val());
  });
  el.closest('body').find('.total-row').val(totalRow);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-2">
  <label>@lang('dashboard.pastyear'):</label>
  <input type="number" value="0" class="form-control bgpending" name="bgpending[]" />
</div>
<div class="col-md-2">
  <label>@lang('dashboard.warida'):</label>
  <input type="number" value="0" class="form-control registered" name="registered[]" />
</div>
<div class="col-md-2">
  <label>@lang('dashboard.total'):</label>
  <input type="text" class="form-control total-row" />
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Your variable get null value or NaN just asign o if it is nan or empty

if(totalRow=='' || totalRow ==NaN )
{
  totalRow=0;  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.