Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Update to a Java Calculator.
Old 12-12-2009, 03:06 PM Update to a Java Calculator.
Junior Talker

Posts: 2
Name: Bernie
Trades: 0
Hi All,
Let me start by saying... I'm a newb!
I was searching the web for a simple price calculator that I could apply to a forms page that I am creating. On this forum, I found a great post by Paul Davis (willcode4beer) where Paul helped someone who was trying to make a calculator for a custom made wooden frame. I have edited that script slightly to suite my needs, but there is one more thing I'm trying to do that I can't quite get done.

---------------------------
Here is a copy of what I have that works well just as it is.
---------------------------
HTML Code:
<html>
<head>
<title>Price Calculator</title>

<style type="text/css">
label {
    display: block;
    float: left;
    width: 6em;
}
</style>

<script type="text/javascript">// <![CDATA[
var baseFee = 10;
var assemblyFee = 5;


function calculate_total(formObj){
    var baseFee = new Number(formObj.baseFee.value);
    var assemblyFee = new Number(formObj.assemblyFee.value);
    var select01 = 0;
    var opts01 = formObj.partSelect01.options;
    /* Find the value of the selected option */
    for(var i=0; i<opts01.length;i++){
        if(opts01[i].selected){
            select01 = new Number(opts01[i].value);
        }
    }
    /* Calculating Total*/
    var basePrice = baseFee + select01;
    var price = basePrice + assemblyFee;
    formObj.total.value = price;
}
// ]]></script>
</head>
<body>
<form name="calc" id="calc">
    <div>
        <label for="baseFee">Base Fee:</label>
        <input type="text" name="baseFee" value="10" disabled="disabled"/>
    </div>
    <div>
        <label for="partSelect01">Part Select 01</label>
        <select name="partSelect01" onchange="calculate_total(this.form)">
            <option value="0" selected>Select your part</option>
            <option value="10.00">Part Option 1 [10.00]</option>
            <option value="20.00">Part Option 2 [20.00]</option>
            <option value="30.00">Part Option 3 [30.00]</option>
        </select>
    </div>
    <div>
        <label for="assemblyFee">Assembly Fee:</label>
        <input type="text" name="assemblyFee" value="5.00" disabled="disabled"/>
    </div>
    <div>
        <label>Total:</label>
        <input id="total" name="total" value="15"/>
    </div>
</form>

</body>
</html>
-----------------------------------
Now, Here is my problem. When I try to add multiple drop down boxes, then edit the script to also calculate these new options it doesn't work.
Also, Please note that in the future I could potentially have 10-20 drop down options that I will want to add in.
-----------------------------------
HTML Code:
<html>
<head>
<title>Price Calculator 2</title>

<style type="text/css">
label {
    display: block;
    float: left;
    width: 6em;
}
</style>

<script type="text/javascript">// <![CDATA[
var baseFee = 10;
var assemblyFee = 5;


function calculate_total(formObj){
    var baseFee = new Number(formObj.baseFee.value);
    var assemblyFee = new Number(formObj.assemblyFee.value);
    var select01 = 0;
    var opts01 = formObj.partSelect01.options;
    /* Find the value of the selected option */
    for(var i=0; i<opts01.length;i++){
        if(opts01[i].selected){
            select01 = new Number(opts01[i].value);
    var select02 = 0;
    var opts02 = formObj.partSelect02.options;
    /* Find the value of the selected option */
    for(var x=0; x<opts02.length;x++){
        if(opts02[x].selected){
            select02 = new Number(opts02[x].value);
    var select03 = 0;
    var opts03 = formObj.partSelect03.options;
    /* Find the value of the selected option */
    for(var y=0; y<opts03.length;y++){
        if(opts03[y].selected){
            select03 = new Number(opts03[y].value);
        }
    }
    /* Calculating Total*/
    var basePrice = baseFee + select01 + select02 + select03;
    var price = basePrice + assemblyFee;
    formObj.total.value = price;
}
// ]]></script>
</head>
<body>
<form name="calc" id="calc">
    <div>
        <label for="baseFee">Base Fee:</label>
        <input type="text" name="baseFee" value="10" disabled="disabled"/>
    </div>
    <div>
        <label for="partSelect01">Part Select 01</label>
        <select name="partSelect01" onchange="calculate_total(this.form)">
            <option value="0" selected>Select your part</option>
            <option value="10.00">Part Option 1 [10.00]</option>
            <option value="20.00">Part Option 2 [20.00]</option>
            <option value="30.00">Part Option 3 [30.00]</option>
        </select>
    </div>
    <div>
        <label for="partSelect02">Part Select 02</label>
        <select name="partSelect02" onchange="calculate_total(this.form)">
            <option value="0" selected>Select your part</option>
            <option value="10.00">Part Option 1 [10.00]</option>
            <option value="20.00">Part Option 2 [20.00]</option>
            <option value="30.00">Part Option 3 [30.00]</option>
        </select>
    </div>
    <div>
        <label for="partSelect03">Part Select 03</label>
        <select name="partSelect03" onchange="calculate_total(this.form)">
            <option value="0" selected>Select your part</option>
            <option value="10.00">Part Option 1 [10.00]</option>
            <option value="20.00">Part Option 2 [20.00]</option>
            <option value="30.00">Part Option 3 [30.00]</option>
        </select>
    </div>
    <div>
        <label for="assemblyFee">Assembly Fee:</label>
        <input type="text" name="assemblyFee" value="5.00" disabled="disabled"/>
    </div>
    <div>
        <label>Total:</label>
        <input id="total" name="total" value="15"/>
    </div>
</form>

</body>
</html>
----------------------------------
Got any ideas? Am I close or am I way off base on how to do this?
Any help would be greatly appreciated.
Thank You for your time.
Bernie

Last edited by chrishirst; 12-12-2009 at 05:46 PM.. Reason: delimiters added
Bern25 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-13-2009, 08:08 AM Re: Update to a Java Calculator.
Junior Talker

Posts: 2
Name: Bernie
Trades: 0
Hello All,

I just figured it out! And feel a little humbled that I forgot something so basic. All I had to do was close the brackets.
Sorry for the waste of time.
FYI... I have included the corrected part of the script below.

Thanks Again.
Bernie

Code:
    var select01 = 0;
    var opts01 = formObj.partSelect01.options;
    /* Find the value of the selected option */
    for(var i=0; i<opts01.length;i++){
        if(opts01[i].selected){
            select01 = new Number(opts01[i].value);
            }
    }
    var select02 = 0;
    var opts02 = formObj.partSelect02.options;
    /* Find the value of the selected option */
    for(var i=0; i<opts02.length;i++){
        if(opts02[i].selected){
            select02 = new Number(opts02[i].value);
            }
    }
    var select03 = 0;
    var opts03 = formObj.partSelect03.options;
    /* Find the value of the selected option */
    for(var i=0; i<opts03.length;i++){
        if(opts03[i].selected){
            select03 = new Number(opts03[i].value);
            }
    }

Last edited by chrishirst; 12-13-2009 at 08:27 AM..
Bern25 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Update to a Java Calculator.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10311 seconds with 12 queries