[REQ.] Need Calculator Scripts To Embed In Sites

anshulmathur

Power Member
Joined
Nov 5, 2014
Messages
584
Reaction score
194
From your first link:

Form inputs are calling javascript function "calculate_volume" on every change
Code:
<input id="length" placeholder="length" onchange="calculate_volume();" onkeyup="calculate_volume();" title="length">

And this is the calculation function in source code:
Code:
function calculate_volume(){
var type=otype.value;
var length=olength.value.trim();

var re = new RegExp(/[1-9][0-9]*\/[1-9][0-9]*/);
if (re.test(length)){length=Fraction2Decimal(length);}

if (length=='' || isNaN(length)){
  ovolume.value='';
  odivMsg.innerHTML='Input radius or diameter length to calculate the volume of a sphere.';
} else {
  var uco=unit_convert_operation();
  if (otype.value=='diameter'){length=length/2;}
  if (ounit.value!=ounit2.value){
    uco=unit_convert_operation();
    if(uco[0]=='*'){length*=uco[1];}else{length/=uco[1];}
  }
  volume = (4/3) * Math.PI * Math.pow(length, 3);
  if (ounit2.value=='m' || ounit2.value=='yd'){d=1000;}else{d=100;}
  ovolume.value=Math.round(volume*d)/d;
  odivMsg.innerHTML='The volume of a sphere with a '+otype.value +' of '+ olength.value +' '+
  unit_text(ounit.value,olength.value) + ' is '+ovolume.value +' cubic '+unit_text(ounit2.value,ovolume.value);
}

So you need to run calculations on every change and keypress inside form ("onchange" and "onkeyup" events) and put results into proper fields
 
Back
Top