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); } });
Comments
Post a Comment