JQuery sum on change keyup () and output in text box

I have the following jQuery code to find the sum of all the rows of a table, now it finds the sum on the keyboard. I want the amount already calculated. I wanted to have a hidden field as an input with a value, so the amount is automatically calculated.

Now there is an input, the user writes a certain number, and how he gets the amount, I try to get the amount automatically. it works, but I need print output in the text box .... for this code

<table>
    <tr>
        <td width="40px">1</td>
        <td>Number</td>
        <td><input class="txt" type="text" name="txt" /></td>
    </tr>
    <tr>
        <td>2</td>
        <td>Number</td>
        <td><input class="txt" type="text" name="txt" /></td>
    </tr>
    <tr>
        <td>3</td>
        <td>Number</td>
        <td><input class="txt" type="text" name="txt" /></td>
    </tr>
    <tr id="summation">
        <td  colspan ="2" align="right">
            Sum :
        </td>
        <td>
            <span id="sum"><input class="txt" type="text" name="txt" />
        </td>
        </span>
        </td>
    </tr>
</table>
$(document).ready(function() {
    //this calculates values automatically 
    calculateSum();

    $(".txt").live("keydown keyup", function() {
        calculateSum();
    });
});

function calculateSum() {
    var sum = 0;
    //iterate through each textboxes and add the values
    $(".txt").each(function() {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            sum += parseFloat(this.value);
            $(this).css("background-color", "#FEFFB0");
        }
        else if (this.value.length != 0){
            $(this).css("background-color", "red");
        }
    });
    $("#sum").html(sum.toFixed(2));
}
+4
source share
2 answers

Use val () to set the value in the text box as follows:

$("#sum input").val(sum.toFixed(2));

Html:

<table>
    <tr>
        <td width="40px">1</td>
        <td>Number</td>
        <td><input class="txt" type="text" name="txt" /></td>
    </tr>
    <tr>
        <td>2</td>
        <td>Number</td>
        <td><input class="txt" type="text" name="txt" /></td>
    </tr>
    <tr>
        <td>3</td>
        <td>Number</td>
        <td><input class="txt" type="text" name="txt" /></td>
    </tr>
    <tr id="summation">
        <td  colspan ="2" align="right">
            Sum :
        </td>
        <td>
          <input type="text" id='sum1' name="input" />
        </td>
        </span>
        </td>
    </tr>
</table>

Jquery:

$(document).ready(function() {
    //this calculates values automatically 
    calculateSum();

    $(".txt").on("keydown keyup", function() {
        calculateSum();
    });
});

function calculateSum() {
    var sum = 0;
    //iterate through each textboxes and add the values
    $(".txt").each(function() {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            sum += parseFloat(this.value);
            $(this).css("background-color", "#FEFFB0");
        }
        else if (this.value.length != 0){
            $(this).css("background-color", "red");
        }
    });

    $("input#sum1").val(sum.toFixed(2));
}

Fiddle DEMO

+8

. val() :

$("#sum input").val(sum.toFixed(2));

:

$("#sum").html(sum.toFixed(2));
+1

Source: https://habr.com/ru/post/1536756/


All Articles