Could anybody give me a piece of code that will show the answer of a quantity entered by a user, multiplyed by a set price/number. I would like the total to automatically be shown. Been struggling with this for a while now!
A bit of help would be great.
Sorry to be an idiot about this but i'v used java before but not much javascript. I want to enter this into a table. The code i have for it at the mo is:
Take note that id should be unique so you need to specify different ids for other totals. To make the price fixed to 2 decimal places, use the script that can be found in the sticky FAQ in this forum.
"Iv been trying to get the "onchange="alert(parseFloat(this.value));"" funtion to work in the total field so as the answer will appear automatically but can't seem to make it work. When i enter the answer into the quantity testbox i have to click to one side to get the answer to appear in the total field. Tried making the page refresh but this does no work either?? Any ideas?"
DONT MIND ALL THAT! just created a fake button called view total, works perfectly.
Could anyone help me on getting an Overall total function working for this. Tryed coding this but its pretty complicated and hard to get a hang of. I know its just a matter of adding all the total varibles together total1+total2..etc., but can't do it.
// Form Compendium f2 (15-04-2005)
// by Vic Phillips http://www.vicsjavascripts.org.uk
// Multiply a 'source' Text Box value with a number and display the result in a 'result' text box
// Any number of applications can exist on a page.
// Application Notes
// the 'source' text box must be allocated a onkeyup event call
// onkeyup="f2_CalTotal(this,*Multiplier*);"
// where *Multiplier* = the multipier number
// The 'result' text box id is specified as the title of the source text box
// e.g.
// <input size=7 title="JoeResult" name="Quantity" value="" onkeyup="f2_CalTotal(this,3.5);" >
// <input size=1 id="JoeResult" value=" " onkeypress="return false;" style="border:0px;background-Color:#FFFFCC;" >
// If a number of 'result' text box values are to be added in a 'Total' text box
// The 'Total' id must be allocated a unique id
// the title of the 'source' text box includes the 'Result' id and the 'Total' id separated with a comma
// e.g.
// <input size=7 title="FredResult,Total" name="Quantity" value="" onkeyup="f2_CalTotal(this,3.5);" >
// <input size=1 id="FredResult" value=" " onkeypress="return false;" style="border:0px;background-Color:#FFFFCC;" >
// The 'Total' must also be initialised by a <body> onload event
// e.g. <body onload="f2_InitTotal('Total');f2_InitTotal('TotalA');" >
// If the sum of the 'Total' text boxes is to be displayed as a 'GrandTotal'
// A text box must be specified with an id of 'f2_GrandTotal' to display the sum.
// All variable, function etc. names are prefixed with 'f2_' to minimise conflicts with other javascripts
// Functional Code
// No Need to Change
var f2_Obj,f2_Mul,f2_Add,f2_TotalVals,f2_ResultObj,f2_TotalObj,f2_Find;
var f2_ObjAry=new Array();
function f2_InitTotal(id){
f2_ObjAry[f2_ObjAry.length]=new Array();
f2_ObjAry[f2_ObjAry.length-1][0]=0;
f2_ObjAry[f2_ObjAry.length-1][1]=id;
}
function f2_CalTotal(obj,mul){
f2_Obj=obj;
f2_Mul=mul;
if (isNaN(obj.value)){
alert('Only Numbers Allowed');
obj.value=(obj.value.substring(0,obj.value.length-1));
return;
}
f2_ResultObj=f2_gEBId(obj.title.split(',')[0]);
f2_ResultObj.value=(obj.value*mul).toFixed(2);
f2_ResultObj.size=obj.value.length+3;
f2_Find=-1
for (i0=0;i0<f2_ObjAry.length;i0++){
if (f2_ObjAry[i0][1]==obj.title.split(',')[1]){
f2_Find=i0;
}
}
if (f2_Find<0){ return; }
f2_Add=1;
for (i1=1;i1<f2_ObjAry[f2_Find].length;i1++){
if (f2_ResultObj==f2_ObjAry[f2_Find][i1]){
f2_Add=0;
}
}
if (f2_Add){
f2_ObjAry[f2_Find][f2_ObjAry[f2_Find].length]=f2_ResultObj;
obj.onkeypress=function(event){ f2_RetnKey(event); }
}
f2_TotalVals=0;
for (i=2;i<f2_ObjAry[f2_Find].length;i++){
f2_TotalVals+=parseFloat(f2_ObjAry[f2_Find][i].value);
}
f2_TotalObj=f2_gEBId(obj.title.split(',')[1]);
f2_TotalObj.value=f2_TotalVals.toFixed(2);
f2_TotalObj.size=f2_TotalVals.toString().length+2;
f2_ObjAry[f2_Find][0]=parseFloat(f2_TotalObj.value);
if (f2_gEBId('f2_GrandTotal')){
f2_TotalVals=0;
for (f2_2=0;f2_2<f2_ObjAry.length;f2_2++){
f2_TotalVals+=f2_ObjAry[f2_2][0];
}
f2_gEBId('f2_GrandTotal').value=f2_TotalVals.toFixed(2);
f2_gEBId('f2_GrandTotal').size=f2_TotalVals.toString().length+2;
}
}
function f2_gEBId(id){
return document.getElementById(id);
}
function f2_RetnKey(e){
if (!document.all){
if (e.which!=8){return; }
}
else {
if (event.keyCode!=8){return; }
}
f2_CalTotal(f2_Obj,f2_Mul)
}