-2

I have wrote a code to split the input into two variables i.e. year and month. But, I am unable to make it work. It does not return the total number of months into the respective text field. Please help me debug my code.

<html>
 <body>
  <table>
   <tr>
    <td>Calculate Months</td>
    <td>
    <label>Input Years in the format (year.month e.g. 11.6)</label>
    <input class="form-control" name="duration" id="duration" value="" type="number"/>
    <br/>
    <label>Total Months</label>
    <input class="form-control" name="totalNumMonths" id="totalNumMonths" value="" type="number"/>
    </td>
   </tr>


  </table>
 </body>
</html>

<script type="text/javascript">
$(function () {


    $("#duration").keyup(function () {


        var input = document.getElementById('duration').value;

        var fields = input.split('.');

        var years = fields[0]; 

        var months = fields[1];

        var result = years.val() * 12 + months.val();

        document.getElementById("totalNumMonths").innerHTML = result; 


  });


});

</script>
Jack Yuan
  • 72
  • 11

1 Answers1

-1

var input = '11.9';

var fields = input.split('.');

var years = fields[0]; var months = fields[1];