Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate total w/ dropdown list values

Status
Not open for further replies.

lahddah

Programmer
Jan 2, 2003
109
US
I'm thinking this is much more simple than I am making it, but I would like to calculate a trip value based on the trip chosen (from a dropdown list), then number of adults, and the number of children. Each item in the drop down list will have a different monetary value. Can someone please tell me how to assign a monetary value, then multiply it by the number of people going to come up with a total for the trip? I'm assuming I would use an array, but am not sure how to incorporate it. Here is an example page:

Thanks!

~ lahddah
 
Your best bet is to put the value into each option of your select box.

Instead of:
<OPTION VALUE=&quot;Nantahala 1 person rental (duckie)&quot;>
Nantahala 1 person rental (duckie)</OPTION>

Use:
<OPTION VALUE=&quot;Nantahala 1 person rental (duckie),49.95&quot;>
Nantahala 1 person rental (duckie)</OPTION>

Then if you pass calculate() the value of select menu triptype, use the following code:
values = triptypeValue.split(&quot;,&quot;);
tripTxt = values[0];
tripVal = parseFloat(values[1]);

Once you have that trip val, the calculating should be straightforward.

Cheers,
-Nm
 
you need to call calculate(this.value) instead of calculate();

then define it as
function calculate(triptypeValue)
 
Okay - I'm sure I'm goofing things up. Now, the value is only equal to '1'. So the total equals the number of people that are entered.

Right now, I have this as a calculation:
total = &quot;&quot; + Math.round(people*values)/100;

But I've also tried to multiply people by tripVal - then I get the NaN message. I thought the tripVal was a number, so I'm confused.



~ lahddah
 
Lahddah,

I made a couple of small changes to your code and get the calculations I believe you wanted. I also set the amount to have two decimal places with the .toFixed() method. Try this and see if it is what you are looking for...

function calculation(triptypeValue) {
// -- Thanks to Owl from the PageResource.com/JavaScript
// -- City support forums for help with the math behind
// -- this script.
values = triptypeValue.split(&quot;,&quot;);
tripTxt = values[0];
tripVal = parseFloat(values[1]);

var people = 0;
if ((document.resform.adults.value != '') || (document.resform.youth.value != ''))
{
people = (parseInt(document.resform.adults.value) + parseInt(document.resform.youth.value));
total = &quot;&quot; + (Math.round(people*values)/100).toFixed(2);
document.resform.ccdeposit.value = &quot;$&quot; + total;
}
}

Let us know,
Brian
 
Ok that didn't fix your problem, I just now fully understand what you were saying.. scratch that last post it. Sorry, still working on it though..

Brian
 
Ok got it working, try this function...

function calculation() {
// -- Thanks to Owl from the PageResource.com/JavaScript
// -- City support forums for help with the math behind
// -- this script.
var people = 0;
if ((document.resform.adults.value != '') || (document.resform.youth.value != ''))
{
values = document.resform.triptype.options[document.resform.triptype.selectedIndex].value.split(&quot;,&quot;);
Txt = values[0];
tripVal = values[1];
people = (parseInt(document.resform.adults.value) + parseInt(document.resform.youth.value));
total = &quot;&quot; + (Math.round(people*tripVal)/100).toFixed(2);
document.resform.ccdeposit.value = &quot;$&quot; + total;
}
}


I would also suggest you change these two lines in your HTML..

<SELECT NAME=&quot;triptype&quot; SIZE=&quot;1&quot; onchange=&quot;return calculation()&quot;;>
<OPTION VALUE=&quot;0,0&quot;>Choose a Trip</OPTION>

This will recalculate the function if a person has already set the number of people and wish to select another trip. It also will Keep it from displaying NaN if they reset it to &quot;Choose a Trip&quot;.

Good Luck With it,

Brian
 
sdi....pretty! Thank you SO much for your help. Works great!

~ lahddah
 
Okay - problem. This script doesn't work in Netscape. Any suggestions?

~ lahddah
 
I don't have netscape but I believe the problem may simply be the one line that ends with .toFixed(2) .. toFixed() may be an IE only method.. remove it from that line and see if it works for you. If it does then you may have to write code to get your 2 digits for the math..

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top