Total value outside code

Asked

Viewed 42 times

1

I have this code below, which is doing sum as I want, but I would like it to generate total values, like:

TOTAL ENTRY = R$ 200
TOTAL EXIT = R$ 50
TOTAL GENERAL = R$ 150

I wish it was outside the code of javascript, type between the body. Could anyone help me in this question? Below is the example:

$(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 />");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

http://jsfiddle.net/1qufobt3/1/

  • Wanted values outside the javascript type code inside a div

  • Please, Hemerson, give us more details of your problem. What you specifically want and an example of the desired result will help a lot.

  • possible duplicate of Add input and output and subtract

No answers

Browser other questions tagged

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