JavaScript Jquery calculation issue (returning NaN)

I have a form in which certain calculations are happening as per the dropdown selected. Everything is working fine except that when i select a value from dropdown price/unit field has to be filled by calculating (total1/qty) which is returning NaN. Pls help on this? My markup is below:

 <html>

 <head>
 <title>Dept</title>
   <script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>

 <script type='text/javascript'>//<![CDATA[
 $(window).load(function(){
 $(document).ready(function(){
  $("select").on('change',function () {
    var dept_number = $(this).val();
    var price = $(this).find(':selected').data('price');

 $(this).closest('table').find('.deptip').val(dept_number);
 $(this).closest('table').find('.priceip').val(price);
 $(this).closest('table').find('.total1').val(price * $(this).closest('table').find('.total').data('value'));
  $(this).closest('table').find('.price_unit').val($(this).closest('table').find('.total1').data('value') / $(this).closest('table').find('.qty').data('value'));

  });
  });
  });//]]> 




 </script>

 </head>

  <body>
  <div class="base"> <!--Keep each group in base container-->
  <table border="1"><tr>
   <td><select id="category0">
   <option value="1" data-price="10">USD</option>
   <option value="2" data-price="20">INR</option>
   <option value="3" data-price="30">AUD</option>
    </select></td>
    <td><label>Dept. num:</label>
     <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
     <td><label>Price:</label>
     <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
     <td><label>Total:</label><input data-value="50" type="text" readonly class="total" value="50"/></td>
     <td><label>Qty:</label><input type="text" class="qty" value="5"/></td>
     <td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
    <td><label>Price/Unit:</label><input type="text" readonly class="price_unit" /></td>


  </tr></table>
  </div>

  <div class="base"> <!--Keep each group in base container-->
  <table border="1"><tr>
  <td><select id="category1">
   <option value="1" data-price="10">USD</option>
   <option value="2" data-price="20">INR</option>
   <option value="3" data-price="30">AUD</option>
   </select></td>
    <td><label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
     <td><label>Price:</label>
     <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
     <td><label>Total:</label><input data-value="50" type="text" readonly class="total" value="50"/></td>
       <td><label>Qty:</label><input type="text" class="qty" value="5" /></td>
      <td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
      <td><label>Price/Unit:</label><input type="text" readonly class="price_unit"/></td>

   </tr></table>
  </div>

  <div class="base"> <!--Keep each group in base container-->
  <table border="1"><tr>
   <td><select id="category2">
   <option value="1" data-price="10">USD</option>
   <option value="2" data-price="20">INR</option>
   <option value="3" data-price="30">AUD</option>
</select></td>
<td><label>Dept. num:</label>
<input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
<td><label>Price:</label>
<input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
<td><label>Total:</label><input data-value="50" type="text" readonly class="total" value="50"/></td>
    <td><label>Qty:</label><input type="text" class="qty" value="5" /></td>
<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
    <td><label>Price/Unit:</label><input type="text" readonly class="price_unit"/></td>


 </tr></table>
  </div>
 </body>

 </html>
Answer:1

Your .qty input has a value and you are trying to get its data-value

Try this

$("select").on('change', function () {
    var dept_number = $(this).val();
    var price = $(this).find(':selected').data('price');
    $(this).closest('table').find('.deptip').val(dept_number);
    $(this).closest('table').find('.priceip').val(price);
    $(this).closest('table').find('.total1').val(price * $(this).closest('table').find('.total').data('value'));
    $(this).closest('table').find('.price_unit').val($(this).closest('table').find('.total1').data('value') / $(this).closest('table').find('.qty').val());

});

https://jsfiddle.net/zu6uvzxv/2/

Answer:2

sorry but I'm new in javascrit. I'm writing a simple rss reader, but I want to limit the number of feeds to 5 items, because the my target site can have 100 or more. Here the code: $("#mainPage")....

sorry but I'm new in javascrit. I'm writing a simple rss reader, but I want to limit the number of feeds to 5 items, because the my target site can have 100 or more. Here the code: $("#mainPage")....

  1. javascript limit number of characters
  2. javascript limit number of decimals
  3. javascript limit number range
  4. javascript limit number to 2 decimal places
  5. javascript limit number of checkboxes selected
  6. javascript limit number of words
  7. javascript limit number to 1 decimal place
  8. javascript limit number of characters in textbox
  9. javascript limit number of characters in input
  10. javascript limit number
  11. javascript limit number of decimal places
  12. javascript limit number input
  13. javascript limit number length
  14. javascript limit number of digits
  15. javascript limit number of lines in div
  16. javascript limit number to 2 decimals
  17. javascript limit number of lines in textarea
  18. javascript limit number of characters in div
  19. javascript limit number of events
  20. javascript limit number of characters in textarea

Demo link <input type="date" class="datepicker"> $('.datepicker').pickadate({ selectMonths: true, // Creates a dropdown to control month selectYears: 15 // Creates a ...

Demo link <input type="date" class="datepicker"> $('.datepicker').pickadate({ selectMonths: true, // Creates a dropdown to control month selectYears: 15 // Creates a ...

I am facing some issues with the jQuery Chosen plugin. Here are some screenshots: The focused Users input is a jQuery Chosen select input within a tab in a Bootstrap dialog (but this doesn't matter ...

I am facing some issues with the jQuery Chosen plugin. Here are some screenshots: The focused Users input is a jQuery Chosen select input within a tab in a Bootstrap dialog (but this doesn't matter ...

  1. jquery chosen scroll

I am very new to Angular world and I have mostly worked with .NET MVC architecture where there is a clear delineation between entities like Model and Controller. It is as clear as following in my ...

I am very new to Angular world and I have mostly worked with .NET MVC architecture where there is a clear delineation between entities like Model and Controller. It is as clear as following in my ...

  1. concept of model view controller