Hi, i would like to ask something, i have a table like this : c60f5fee223b1398d53631dda019f205

The user will enter a value in the column "out Quantity", then, i want it auto calculate in "Total Price" which is
Total Price = Old balance quantity * out quantity and
new Balance = old balance quantity - out Quantity

how can i do that?

I tried to do it like this, but it won't work :

<script>
function compute() {
    var price = document.getElementById('valuePerUnit');
    var outqty = document.getElementById('k9goodsQty');
    var total = document.getElementById('valueTotal');
    var oldqty = document.getElementById('goodsQty');
    var balance = document.getElementById('qtyBalance');

    var v1=value.value;
    var v2=outqty.value;
    var v3=total.value;
    var v4=oldqty.value;
    var v5=balance.value

    var val1 = v1==="" ? 0 : parseFloat(v1); 
    var val2 = v2==="" ? 0 : parseFloat(v2);
    var val3 = v3==="" ? 0 : parseFloat(v3);
    var val4 = v4==="" ? 0 : parseFloat(v4);
    var val5 = v5==="" ? 0 : parseFloat(v5);    

    jumlah.value = val1 * val2;
    baki.value =  val4 - val2;
    }
</script>
Member Avatar for LastMitch

I tried to do it like this, but it won't work :

@sagisgirl

On line 15 to line 20:

var val1 = v1==="" ? 0 : parseFloat(v1);
var val2 = v2==="" ? 0 : parseFloat(v2);
var val3 = v3==="" ? 0 : parseFloat(v3);
var val4 = v4==="" ? 0 : parseFloat(v4);
var val5 = v5==="" ? 0 : parseFloat(v5);

Why you have an extra equal sign?

Plus how does your html code looks like related to your javascript code?

actually I found the code on the internet(but it is simpler), so I modified a little bit the code..so, Im not sure why there is an extra equal sign.

    <script>
    function compute() {
    var price = document.getElementById('valuePerUnit');
    var outqty = document.getElementById('k9goodsQty');
    var total = document.getElementById('valueTotal');
    var oldqty = document.getElementById('goodsQty');
    var balance = document.getElementById('qtyBalance');

    var v1=value.value;
    var v2=outqty.value;
    var v3=total.value;
    var v4=oldqty.value;
    var v5=balance.value

    var val1 = v1==="" ? 0 : parseFloat(v1);
    var val2 = v2==="" ? 0 : parseFloat(v2);
    var val3 = v3==="" ? 0 : parseFloat(v3);
    var val4 = v4==="" ? 0 : parseFloat(v4);
    var val5 = v5==="" ? 0 : parseFloat(v5);

    total.value = val1 * val2;
    balance.value = val4 - val2;
    }
    </script>

The extra equal sign is there for static-Type comparison, i.e.: to avoid the literal 0 (zero) to evaluate as a Boolean false.

@TroyIII
so can you help me,on how to do it..? please guide me..

of course if you tell me what does sagis-girl mean.

for a start there's a trasncript redoundant word or brain glitch error during transcription on line
9. var v1=value.value;
it should be correspondently equal to:
9. var v1=price.value; //instead

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.