By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

Tricky Addition Question In Javascript...Please help. TIA

P: n/a

I'm extremely puzzled; I cannot figure out what I'm doing wrong. Here's
the situation. I would really appreciate any suggestions.

I'm modifying a shopping cart in the following way. I've just added new
prices to several drop-downs on our javascript-based ordering page. These
new prices must have shipping costs added to them at the bottom of the
ordering system in a text box called "Shipping", while the existing prices
do not have to have shipping costs added.

Basically this means that if the user selects the first 3 options in the
drop-down boxes, the text box of "Shipping" must show "7.00", but if any
option after that is selected, the text box of "Shipping" must show "0.00".
Oh, and here's another wrinkle: only certain drop-down boxes will need this
modification. Also, if a customer selects more than one of these "7.00"
items, an additional "7.00" must be added to their shipping costs.

Any suggestions on how to best implement this? I've tried the following
code, but it isn't working--specifically, I can't the shipping to be
properly affected if a users should move from a "7.00 Shipping" option to a
"0.00" option..

var ShippingTotal = 0;
var ShippingAdded = 7;
var TemporaryShipping = 0;

var NewQuantity1 = "False";
var NewQuantity3 = "False";

....[this is the same code for all drop-down boxes]

if ((document.Form.Item3.selectedIndex == 1) ||
(document.Form.Item3.selectedIndex == 2) ||
(document.Form.Item3.selectedIndex == 3))

if (NewQuantity3=="True")
ShippingTotal += ShippingAdded;
document.Form.Shipping.value = ShippingTotal;
else if (NewQuantity3=="False")
document.Form.Shipping.value = ShippingTotal;

Again, any help would be greatly appreciated. Thanks in advance.

Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
It's a darn tricky problem. Good workout. I did this (which assumes
you are using onChange events and that there is a "none" option at
index 0 for all boxes)

var ShippingTotal = 0;

//arrayItem for every selectbox to store last index selected

var lastItemArray = new Array();
lastItemArray[0] = 0;
lastItemArray[1] = 0;
lastItemArray[2] = 0;

// onChange event function for all select boxes (only one function)
// what is form element object
// which is id num manually passed in for array

function reCalc(what,which) {
lastSelected = lastItemArray[which]
if ((what.selectedIndex>0 && what.selectedIndex<4) &&
!(lastSelected>0 && lastSelected<4)) {
else if ((what.selectedIndex<1 || what.selectedIndex>3) &&
(lastSelected<1 || lastSelected>3)) {
if (ShippingTotal<0) {
document.Form.Shipping.value = ShippingTotal;
Arrays are useful. Hope that helps.

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.