-1

I have a input element to accept number type. I know javascript takes input as a string. so I am using parseInt() to convert into integer. but it is not working.

My code is:

<tr>
    <td rowspan="6"><br><br><br><br><br>B1<br> Salary/Pension:</td>
    <td>(1) Salary (excluding all allowances, perquisites and profit in lieu of salary</td>
    <td><input type="text" id="sal" value="0" name="sal" placeholder="" class="form-control "></td>
</tr>
<tr>
    <td>(2) Allowances not exempt</td>
    <td><input type="text" id="allowance" value="0" name="allowance" placeholder="" class="form-control "></td>
</tr>
<tr>
    <td>(3)Value of perquisites</td>
    <td><input type="text" id="perquisites" value="0" name="perquisites" placeholder="" class="form-control "></td>

</tr>
<tr>
    <td>(4) Profits in lieu of salary</td>
    <td><input type="text" id="profit" name="profit" value="0" placeholder="" class="form-control"></td>
</tr>
<tr>
    <td>(5)Deduction u/s 16</td>
    <td><input type="text" id="ded16" name="ded16" value="0" placeholder="" class="form-control"></td>
</tr>
<tr>
    <td>(6)Income chargable under the head 'Salaries':</td>
    <td><input type="text" id="inchargesal" value="0" name="inchargesal" placeholder="" class="form-control" readonly></td>
</tr>

//js part is:

  $(document).ready(function() {
    function change() {
        f = a + b + c + d - e;
        alert(f);
        $('#inchargesal').removeAttr('readonly').val(f);
    }
    $('#sal').on('change', function() {

        var a = parseInt(document.getElementsById("sal").value);
        alert("hi");
        change();
    });
    $('#allowance').on('change', function() {
        b = parseInt(document.getElementById("allowance").value)

        change();
    });
    $('#perquisites').on('change', function() {
        c = parseInt(document.getElementsById("perquisites").value);
        change();
    });
    $('#profit').on('change', function() {
        d = parseInt(document.getElementsById("profit").value);
        change();
    });
    $('#ded16').on('change', function() {
        e = parseInt(document.getElementsById("ded16").value);
        change();
    });
});

here to notice is, when I use alert("hi") above the parseInt statement then alert works fine, however when I use it after that, alert("hi") doesn't work.

what's going wrong? please help.

Andrea Carraro
  • 9,731
  • 5
  • 33
  • 57

1 Answers1

2

You are using incorrect method of document. There is no such method saying getElementsById(). In HTML DOM the id field is unique so it can never be getElements but getElement. Use getElementById('id') to make it work.

Also its always better to check console first where most of your problems can be resolved on your own. Console - enter image description here

Ashvin777
  • 1,446
  • 13
  • 19