html - Calculate percentage Javascript -


i have question javascript logic use percent of 2 inputs text fields. here code:

    var ppos = $('#pointspossible').val();     var pearned = $('#pointsgiven').val();      var perc = ((pearned/ppos) * 100).tofixed(3);     $('#pointsperc').val(perc); 

for reason if inputs 600 , 200, result suppose 33.333 i'm getting 3.333. if hard code values works fine. if can appreciate that. in advance.

it seems working :

html :

 <input type='text' id="pointspossible"/> <input type='text' id="pointsgiven" /> <input type='text' id="pointsperc" disabled/> 

javascript :

    $(function(){      $('#pointspossible').on('input', function() {       calculate();     });     $('#pointsgiven').on('input', function() {      calculate();     });     function calculate(){         var ppos = parseint($('#pointspossible').val());          var pearned = parseint($('#pointsgiven').val());         var perc="";         if(isnan(ppos) || isnan(pearned)){             perc=" ";            }else{            perc = ((pearned/ppos) * 100).tofixed(3);            }          $('#pointsperc').val(perc);     }  }); 

demo : http://jsfiddle.net/vikashvverma/1khs8sj7/1/


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -