anup dwivedi February 2016

Sum subtract not work in all input fields

How i use this js for sum and subtract for all input field ,when i use this its work for only 1st field only any one help me out for this.

 $(document).ready(function() {
    //this calculates values automatically 
    sum();
    $("#num1, #num2").on("keydown keyup", function() {
        sum();
    });
});

function sum() {
    var num1 = document.getElementById('num1').value;
    var num2 = document.getElementById('num2').value;
	var result = parseInt(num1) + parseInt(num2);
	var result1 = parseInt(num2) - parseInt(num1);
    if (!isNaN(result)) {
          document.getElementById('sum').value = result;
		  document.getElementById('subt').value = result1;
    }
}

Answers


abhay vyas February 2016

this is working...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){

        $("#t1,#t2").keyup(function(){
                var t1=parseInt($("#t1").val());
                var t2=parseInt($("#t2").val());
                var t3=t1+t2;
                var t4=t1-t2;
                $("#t3").val(t3);  
                $("#t4").val(t4);  
        });
    });
    </script>
    <input type="text" id="t1" value="0">
    <input type="text" id="t2" value="0">
    <input type="text" id="t3">
    <input type="text" id="t4">

Post Status

Asked in February 2016
Viewed 2,992 times
Voted 10
Answered 1 times

Search




Leave an answer