0

i'd like to ask how to do arithmatic using javascript Here i have some input value

<div class="form-group">
    <label for="exampleInputTotalBayar">Total Bayar</label>
    <input type="text" class="form-control" id="inputTotalBayar" placeholder="Total Bayar" name="totalBayar" value="{{ $peminjaman->totalBayar }}">
</div>
<div class="form-group">
    <label for="exampleInputJumlahBayar">Telah Membayar</label>
    <input type="text" class="form-control" id="inputJumlahBayar" onchange="" placeholder="Jumlah Bayar" name="jumlahBayar" value="{{ $peminjaman->jml_bayar }}">
</div>

I have this input too

<div class="form-group">
    <label for="exampleInputDenda">Denda</label>
    <input type="text" class="form-control" id="exampleInputDenda" placeholder="Denda" name="denda" value="{{ $peminjaman->denda }}">
</div>

And this

<div class="form-group">
    <label for="exampleInputBayarKembali">Jumlah Bayar Pengembalian</label>
    <input type="text" class="form-control" id="inputBayarKembali" onchange="" placeholder="Bayar Pengembalian" name="bayarPengembalian">
</div>

And using javascript, the answer from arithmatic operation will be shown here

<p>Bayar Pengembalian: <div id="bayar" name="bayar"></div></p>
<p>Sisa: <div id="sisa" name="sisa"></div></p>

For Bayar Pengembalian, it work just fine. But when im trying to calculate Sisa, the answer wont be shown. Here is my javascript

$("#exampleInputDenda").change(function() {
        var bayar = parseInt("0");
        var totalBayar = parseInt($("#inputTotalBayar").val());
        var jumlahBayar = parseInt($("#inputJumlahBayar").val());
        var denda = parseInt($("#exampleInputDenda").val());
        bayar = bayar + denda + totalBayar - jumlahBayar;
        $("#bayar").html(bayar);
    });

    $("#inputBayarKembali").change(function() {
        var sisa = parseInt("0");
        var bayar = $("#bayar").val();
        sisa = sisa + $("#inputBayarKembali").val()-bayar;
        $("#sisa").html(sisa);
    });

would you help me with my problem? I'm just in the middle of studying javascript and laravel, so i really need your help... thanks before

3
  • what errors do you get (e.g console output) Commented Sep 14, 2014 at 11:43
  • why dont you use parseInt() for second changehandler ? Commented Sep 14, 2014 at 11:44
  • it cant reach the value for bayar Commented Sep 14, 2014 at 11:50

1 Answer 1

2

You need to parseInt() around the input values:

$("#inputBayarKembali").change(function() {
    var sisa = parseInt("0");
    var bayar = parseInt($("#bayar").text());
    sisa = sisa + parseInt($("#inputBayarKembali").val())-bayar;
    $("#sisa").html(sisa);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Oh thank you... can i ask you something? why we need .text() when identifying var bayar?
Because .val() is for returning the value of inputs. And .text() or .html() returns the content of other elements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.