Run JavaScript Function onkeyup not working -


i got old code of apps using javascript calculate something, logic if 1 textbox filled, textbox automatic filled, , when second textbox filled, textbox return text1 - text2, maybe can @ fiddle:

html:

<label>price start</label> <input type="number" class="form-control" placeholder="total harga diberikan" required="required" name="price" onkeyup="calculate()" id="price" /> <br /> <label>discount</label> <input type="number" class="form-control" placeholder="nominal potongan" name="jmlvoucher" onkeyup="calculate()" id="jmlvoucher" /> <br /> <label>total</label> <input type="number" class="form-control" placeholder="total yang harus dibayarkan" required="required" name="total" id="total" /> 

javascript:

function calculate(){     var num1 = document.getelementbyid("price").value;     var num2 = document.getelementbyid("jmlvoucher").value;      var sum=parsefloat(num1)-parsefloat(num2);     document.getelementbyid('total').value=sum.tostring(); } 

http://jsfiddle.net/n4anv5qn/2/

already try check error, there's no error. try run it, doesn't works. idea why?

there indeed error being generated keyup fired.

uncaught referenceerror: calculate not defined 

on jsfiddle you've chosen put javascript onload function wrong. should instead choose no wrap

see http://jsfiddle.net/n4anv5qn/5/

as requested, code example of how can still calculate total if discount not filled. check see if value grabbed num2 blank , set default value.

function calculate(){     var num1 = document.getelementbyid("price").value;     var num2 = document.getelementbyid("jmlvoucher").value;      if (num2 == '')         num2 = '0';      var sum=parsefloat(num1)-parsefloat(num2);     document.getelementbyid('total').value=sum.tostring(); } 

Comments