0

Here i have some some form group with values from database

<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">Jumlah Bayar</label>
    <input type="text" class="form-control" id="inputJumlahBayar" onchange="" placeholder="Jumlah Bayar" name="jumlahBayar" value="{{ $peminjaman->jml_bayar }}">
</div>

Then i want to give new values in this form

<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>

so, when i give new values in form group Denda, the output will be show in here

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

My code for javascript was like this

<script type="text/javascript">

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

but the calculation result doesn't appear, can you help me to solve this problem?

2
  • I find that this shouldn't be ever asked in SO... a quick search on the net would be enough to determine how to perform type conversions........ Commented Sep 10, 2014 at 8:47
  • Put your variable initialization inside your function. Commented Sep 10, 2014 at 8:48

3 Answers 3

2

You have to use

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

Error checking :)

Sign up to request clarification or add additional context in comments.

Comments

0

Just change you event handler so you can get the values when the actual event happen like so - JSFiddle example

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

P.S Andd click Enter to actually see the new value!

Comments

0

see this with slide change of your code:

$(document).ready(function(){

  $("#exampleInputDenda").change(function(){
getValues();
});

// when using click event on button
   $("#me").click(function(){
        getValues();
    });

    });

function getValues(){
    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);

}
</script>

to see on click use this:

<button id="me" onclick='getValues()'>Click me!</button>

Comments

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.