jQuery Sum & Subtract Two Input Fields

jQuery Sum & Subtract Two Input Fields is moved here  http://www.allphptricks.com/jquery-sum-subtract-two-input-fields-values/

 

Advertisements

4 thoughts on “jQuery Sum & Subtract Two Input Fields

  1. Hi, I think the function sum() can be rewritten as follows:
    function sum(){
    $(“#sum”).val(Number($(“#num1”).val())+Number($(“#num2”).val()));
    $(“#subt”).val(Number($(“#num1”).val())-Number($(“#num2”).val()));
    }

    Also, $(“#num1, #num2”).on(“keydown keyup”, function() {
    sum();
    });
    can be rewritten as $(“#num1, #num2”).on(“keydown keyup”,sum);

    Like

  2. Thanks for your input Deepak, i would appreciate if you write your code in Code block.
    For example like this:

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

    For your convenience you can check code block format here
    https://en.support.wordpress.com/markdown-quick-reference/

    Or leave a jsfiddle.net working demo link, as i provided above in my tutorial. 🙂

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s