Add input and output and subtract

Asked

Viewed 575 times

4

I have this code:

$(document).ready(function () {
    var $entrada = 0,
        $saida = 0,
        $total = 0;
    $.each($("td[name='entrada']"), function() {
        $entrada += parseFloat($(this).text().replace(",", "."));
    });
    $.each($("td[name='saida']"), function() {
        $saida += parseFloat($(this).text().replace(",", "."));
    });
    $total = $entrada - $saida;
    $("body").append("TOTAL ENTRADA = R$ " + $entrada + "<br />")
             .append("TOTAL SAIDA = R$ " + $saida + "<br />")
             .append("TOTAL GERAL = R$ " + $total + "<br />");
});
<body>
    <table width="" border="1">
        <tr>
            <td>ENTRADA</td>
        </tr>
    </table>
    <table width="198" border="1" id="table">
        <tr>
            <td width="39%">PRODUTO</td>
            <td width="12%">VALOR</td>
        </tr>
        <tr>
            <td>1</td>
            <td name="entrada">100,00</td>
        </tr>
        <tr>
            <td>2</td>
            <td name="entrada">100,00</td>
        </tr>
    </table>
    <p>&nbsp;</p>
    <table border="1">
        <tr>
            <td>SAIDA</td>
        </tr>
    </table>
    <table width="196" border="1">
        <tr>
            <td width="39%">DESCRICAO</td>
            <td width="12%">VALOR</td>
        </tr>
        <td>SAIDA</td>
        <td name="saida">50,00</td>
        </tr>
    </table>
    <p>&nbsp;</p>
</body>

I need the sums of the total values outside the javascript type within a table or id div

I know this is for those who know javascript someone could help me

  • 1

    I didn’t understand the doubt. You want to add without using javascript?

  • But is your code no longer doing this correctly? o_o

  • 1

    I don’t understand what’s missing either... so you can explain better what you can’t do?

  • I want the totals inside a table as I don’t know much of javascript.

  • you’ve solved that issue ?

1 answer

1

If that’s what I understand you want to put table with javascript, if that’s it then just add javascript as below

$(document).ready(function () {
var $entrada = 0,
    $saida = 0,
    $total = 0;
$.each($("td[name='entrada']"), function() {
    $entrada += parseFloat($(this).text().replace(",", "."));
});
$.each($("td[name='saida']"), function() {
    $saida += parseFloat($(this).text().replace(",", "."));
});
$total = $entrada - $saida;
var $table = $('<table>').attr('border',1);
$table.append('<tr><td>TOTAL ENTRADA</td><td>'+ $entrada +'</td></tr>');
$table.append('<tr><td>TOTAL SAIDA</td><td>'+ $saida +'</td></tr>');
$table.append('<tr><td>TOTAL GERAL</td><td>'+ $total +'</td></tr>');
$("body").append($table);
$("body").append("TOTAL ENTRADA = R$ " + $entrada + "<br />")
         .append("TOTAL SAIDA = R$ " + $saida + "<br />")
         .append("TOTAL GERAL = R$ " + $total + "<br />");
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.