We help IT Professionals succeed at work.

Javascript Order Calculator

bogie asked
Medium Priority
Last Modified: 2008-03-17
I'm looking for a JavaScript order form calculator that will automatically put in the price of an item if only the item is chosen from a drop down box.  Then the only other variable would be the qty. This would create the total for that line item in an order.  Each line could be another item
selected from another drop down box.  All items would then go to a sub-total, tax, shipping and grand total. I saw one once like this where the item designated the price instead of a fixed price per line - just can't remember where.

Thanks in advance.
Herb Bogart
Watch Question

Sorry, I do not know exact rules how you want to calculate
shippment and taxes, (sorry I live in other country :)
but below is example how this can be done
look into comments inside:

// change number of lines(selects) and tax percent here!
var nlines=3;      
var taxpercent=5;
function calc()
{var summ=0;
  {var inx=eval("document.myform.myselect"+i+".selectedIndex");
   var price=eval("document.myform.myselect"+i+".options[inx].value");
   var qty=eval("qty=document.myform.q"+i+".value");


// implement your counting rules here!
//summ refers to Summ of items*qtys

 // rounded tax

 //calculate shipping  $3.00 per item for example
 // finnaly we have



<FORM  NAME="myform"  onSubmit="calc(); return false;">

<!--If you need to add more lines, just copy-->
<!--and paste from here ---------------------->
<SELECT name="myselect0" onChange="calc();">
  <option value="0.0"  >Please, select
  <option value="0.99" >item1
  <option value="1.25" >item2
  <option value="11.50">item3
  <option value="10.00">item4
 Qty:<INPUT TYPE="TEXT" NAME="q0" value="0" size=5 onChange="calc();">
   $:<INPUT TYPE="TEXT" NAME="t0" value="0" size=5><br>

<!---------to here--------->
<!---and Do not forget to change names:-->
<!--  myselect0                        -->
<!--  q0                               -->
<!--  t0                               -->
<!-- to new actual numbers of line     -->

<!-- next  row of select in html       -->
<SELECT name="myselect1" onChange="calc();">
  <option value="0.0"  >Please, select
  <option value="0.99" >item1
  <option value="1.25" >item2
  <option value="11.50">item3
  <option value="10.00">item4
 Qty:<INPUT TYPE="TEXT" NAME="q1" value="0" size=5 onChange="calc();">
   $:<INPUT TYPE="TEXT" NAME="t1" value="0" size=5><br>

<!-- next  row of select in html       -->
<SELECT name="myselect2" onChange="calc();">
  <option value="0.0"  >Please, select
  <option value="0.99" >item1
  <option value="1.25" >item2
  <option value="11.50">item3
  <option value="10.00">item4
 Qty:<INPUT TYPE="TEXT" NAME="q2" value="0" size=5 onChange="calc();">
   $:<INPUT TYPE="TEXT" NAME="t2" value="0" size=5><br>

total:<br><INPUT TYPE="TEXT" name="total" onChange="calc();"><br>
Tax:(5%)<br><input type="TEXT" name="tax"   onChange="calc();"><br>
Shipping($3.00 per item)<br><input type="TEXT" name="shipping"   onChange="calc();"><br>
FinalTotal:<br><input type="TEXT" name="ftotal"   onChange="calc();"><br>
<Input type="button" value="Calculate" onClick="calc();"><br>

Please, let me know if you need changes in calculations
algorythms, I'll do it for you.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

This solution have a difference from previous one
it a little bit easier to change number of lines in form,
And you need to make changes (add/remove items)
in only one place.

// number of lines you want to have;
var nl=3;

function initArray()
{this.length = 0;}

prices  =new initArray();
products=new initArray();

function addElement(pname,pprice)

// add/remove items here!
addElement("--------select from list----------","0");

// you can change change taxes and shipment rules here
// ----------------------------------------------------
function taxesandshipment(totalsumm,itemsnumber)
{return totalsumm*1.05+itemsnumber*0.30;

function calc()
{var summ=0;
 var totalqty=0;
  {var qty;
     {if((qty.charAt(j)<"0") || (qty.charAt(j)>"9"))
             alert("Invalid Qty in Line:"+i);
   var inx;
   var p=prices[inx];
   var t=qty*p;
   if(p!=0) totalqty+=qty;

function setform()
{var s ='<FORM onSubmit="return false">';
  {s='<SELECT name="line'+i+'" onChange="calc()">';
         {s ='<option value="'+prices[j]+'">'+products[j];
   s ='</select>';
   s +=' Qty:<INPUT name="q'+i+'" TYPE="TEXT" onChange="calc()" size=12 value="0">'
   s +=' $:<INPUT name="t'+i+'" TYPE="TEXT" onChange="calc()" size=12 value="0"><br>'
 s='<input type="submit" onClick="calc(); return false;" value="Calculate">';
 s ='<br>total:<br><input type="text" name="total" size=12 value="" onChange="calc()">'
 s+='<br>including 5% tax and $0.30 shipping and handling per item:<br><input type="text" name="tfinal" size=12 value="" onChange="calc()">'
 for(i=0;i<nl;i++) eval('document.forms[0].line'+i+'.options[0].selected=true');



Adjusted points to 250
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.