Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:27:06+00:00 2026-05-28T18:27:06+00:00

I coded a simple form to calculate a price by size, shape, color and

  • 0

I coded a simple form to calculate a price by size, shape, color and quantity using JavaScript. The problem is when I add the values they won’t show up. When I only get the total of the first select box it shows up. But not whne I try to add the totals and the quantity doesn’t seem to work at all. I have tried many different variations of the code but just can’t seem to get it to work.

Here is the HTML for the form.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Price Qoute</title>
  <script type="text/javascript" src="formCalc.js">
  </script>
</head>

<body onload='hideTotal()'>
 <form action="" id="price-quote">
  <select id="size" name="size" onchange="getTotal()">
    <option value="None">Select Size</option>
    <option value="2.5 inches">2.5 inches</option>
    <option value="3 inches">3 inches</option>
  </select>

<select id="shape" name="shape" onchange="getTotal()">
    <option value="None">Select Shape</option>
    <option value="Round">Round</option>
    <option value="Oval">Oval</option>
</select>

<select id="color" name="color" onchange="getTotal()">
   <option value="None">Select Color</option>
   <option value="One Color">One Color</option>
   <option value="Full Color">Full Color</option>
</select>

<label>Quantity</label><input type="text"  name="quantity" id="quantity"          onchange="getTotal()" />

</form>
<div id="totalPrice"></div>
</body>
</html>

and here is the JavaScript

var size_prices= new Array();
size_prices["None"]=0;
size_prices["2.5 inches"]=.10;
size_prices["3 inches"]=.20;

function getSizePrice()
{
    var sizePrice=0;
    var theForm = document.forms["price-quote"];
    var selectedSize = theForm.elements["size"];
    sizePrice = size_prices[selectedSize.value];
    return sizePrice;
}

var shape_prices= new Array();
shape_prices["None"]=0;
shape_prices["Round"]=.10;
shape_prices["Oval"]=.20;

function getShapePrice()
{
    var shapePrice=0;
    var theForm = document.forms["price-quote"];
    var selectedShape = theForm.elements["shape"]; 
    shapePrice = shape_prices[selectedShape.value];
    return shapePrice;
}

var color_prices= new Array();
color_prices["None"]=0;
color_prices["One Color"]=1;
color_prices["Full Color"]=3;

function getColorPrice()
{
    var colorPrice=0;
    var theForm = document.forms["price-quote"];
    var selectedColor = theForm.elements["color"];
    colorPrice = color_prices[selectedColor.value];
    return colorPrice;
}

function getQuantity()
{
    var theForm = document.forms["price-quote"];
    //Get a reference to the TextBox
    var quantity = theForm.elements["quantity"];
    var howmany =0;
    //If the textbox is not blank
    if(quantity.value!="")
    {
        howmany = parseInt(quantity.value);
    }
    return howmany;
}

function getTotal()
{
    //Here we get the total price by calling our function
    //Each function returns a number so by calling them we add the values they return           together
    var instantPrice = getSizePrice() + getShapePrice() + getColorPrice(); 
    //display the result
    document.getElementById('totalPrice').innerHTML =
                                  "$"+instantPrice;
}

What am I doing wrong?

!!!!Update!!!!

Ok I had a typo so I got the form to calculate the prices of the select boxes but still no go on the quantity. I really need to have this update dynamically as the users types (without a button or hitting enter) if at all possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:27:06+00:00Added an answer on May 28, 2026 at 6:27 pm

    A few comments:

    Even though most browsers will make the first option of a select element selected by default, some may not. So alway give the default option the selected attribute.

    You can make things a bit easier by have option values like “2.5” rather than “2.5 inches”.

    In your code:

    > var size_prices= new Array();
    >  size_prices["None"]=0;
    >  size_prices["2.5 inches"]=.10;
    >  size_prices["3 inches"]=.20;
    

    Here you are using an array like an object. It is better to use the right tool for the job, so use an object:

     var size_prices= { 
          'None': 0,
          '2.5 inches': 0.10,
          '3 inches': 0.20
     };
    

    The same goes for the color_prices “array”.

    […]

    >     var sizePrice=0;
    

    If you are not going to use the value assigned, then don’t assign one. You assign a value later, so just declare the variable:

     var sizePrice;
    

    In the getQuantity function:

    > if(quantity.value!="") {
    >      howmany = parseInt(quantity.value);
    >  }
    >  return howmany;
    

    You really should check either the input value as it could be rubbish, or the returned value as it could be NaN (because the input was rubbish). And you should always include a radix wth parseInt in case 09 is entered:

    alert(parseInt('09')); // 0
    alert(parseInt('09', 10)); // 9
    

    In the getTotal function:

    > var instantPrice = getSizePrice() + getShapePrice() + getColorPrice();
    

    Where does the quantity get included?

    Edit

    I don’t know where you are going wrong, using the following:

     var instantPrice = (getSizePrice() + getShapePrice() +
                          getColorPrice()) * getQuantity();  
    

    I get what appears to be the correct value. You need to work on checking the value input for quantity and format the answer to two decimal places correctly though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form that uploads an image to a database. Using a
<SCRIPT Language = JavaScript> function calculate() { a = 12 b = eval(document.form.number.value) c
I am constructing a simple calculator using a form that requires selections from a
I want to code a simple form layout in flex. Something like the following:
I am generating a simple form with php. The following code has been reduced
Client code is pretty simple: <form action=DDServlet method=post> <input type=text name=customerText> <select id=customer> <option
I'm working on a simple tool that transfers files to a hard-coded location with
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I am implementing a fairly simple calendar on a website using PHP and MySQL.
I'm using Visual Basic 2010 Express. I have a form that can be minimized.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.