0

I need to add the OptionPricing variable to the 2: 258 which is in the var AccompPricing, in this script. I want to write something like this 2: 258 + (var Price), but it doesn't work like that. The total package should be added to the second choices value. Example: 258 + The price the package = Total #2.

// Collect Data & Prices To Update Dynamic Prices

var OptionPricing = {

'pack11049': 1049,
'pack21199': 1199,
'pack31199': 1199,
'pack41299': 1299,
'pack51449': 1449,
'pack61499': 1499,
'pack71549': 1549,
'pack81699': 1699,
'pack91799': 1799,
'pack101999': 1999,
'pack112499': 2499,
'pack122549': 2549


};


function checkOptions() {

var Price = 0;

for (Packs in OptionPricing) {

    if ($('#' + Packs).is(':checked')) {           
        Price += OptionPricing[Packs];
    }
}

return Price;
}
var AccompPricing = {
0: 0,
1: 129,
2: 258 + (var Price),
3: 1057,
4: 1856    
};

function checkAccomp() {

var Accomp = parseInt($('#howmany').val(), 10);

return AccompPricing[Accomp];
}

function updateTotal() {

var ThePrice = checkOptions() + checkAccomp();


$('#TotalPrice').text('$' + ThePrice + '.00');

}

$(function () { $('.DoPricing').click(updateTotal); });
5
  • It's really hard to tell what you're trying to do. When do you want to add Price to AccompPricing[2]? Because updateTotal already basically does that. It gets the price back from checkOptions and adds it to the amount from the appropriate index of AccompPricing. So just removing the + var Price from the AccompPricing object initializer would seem sufficient. So it's really not clear why you think you want something there, or what you're trying to do. Commented Jun 13, 2013 at 6:58
  • Well Here is what I am trying to accomplish. The OptionsPricing holds the final value of our radio buttons called PACKAGES. The AccompPricing holds an additional value and we will call that OCCUPANCY PRICE. But only for the 2nd Occupant #2 (2:258) $258 we want to charge them the full price of the package also, but not for #3 or #4. #1 is already getting charged the full package but we need to add the full price again IF there is a second occupant! Make Sense? Commented Jun 13, 2013 at 7:11
  • Not really, no. :-) So you want to do something with the result of checkOptions based on which entry in AccompPricing gets used? Commented Jun 13, 2013 at 7:17
  • I just want to add the final result of OptionsPricing to the #2 option in AccompPricing so the pricing will change. 258 + whatever the final result of OptionsPricing (packages). That way #2 gets charged $258 plus for example $1099 = $1357. But the $1099 will change on selection! Commented Jun 13, 2013 at 7:21
  • $1099 may be $2499 or $1999 etc... Commented Jun 13, 2013 at 7:22

1 Answer 1

1

It sounds like you need a flag on the entries in your AccompPricing object to tell you whether you add the package price on again. Something like this: Live Copy | Live Source

// Collect Data & Prices To Update Dynamic Prices

var OptionPricing = {

    'pack11049': 1049,
    'pack21199': 1199,
    'pack31199': 1199,
    'pack41299': 1299,
    'pack51449': 1449,
    'pack61499': 1499,
    'pack71549': 1549,
    'pack81699': 1699,
    'pack91799': 1799,
    'pack101999': 1999,
    'pack112499': 2499,
    'pack122549': 2549
};

var AccompPricing = {
    0: {Price: 0,     Flag: false},
    1: {Price: 129,   Flag: false},
    2: {Price: 258,   Flag: true},
    3: {Price: 1057,  Flag: false},
    4: {Price: 1856,  Flag: false}
};

function checkOptions() {

    var Price = 0;

    for (Packs in OptionPricing) {

        if ($('#' + Packs).is(':checked')) {           
            Price += OptionPricing[Packs];
        }
    }

    return Price;
}

function checkAccomp() {

    var Accomp = parseInt($('#howmany').val(), 10);

    return AccompPricing[Accomp];
}

function updateTotal() {

    var PackagePrice, Accomp, ThePrice;

    PackagePrice = checkOptions();
    Accomp = checkAccomp();
    ThePrice = PackagePrice + Accomp.Price;
    if (Accomp.Flag) {
        ThePrice += PackagePrice;
    }

    $('#TotalPrice').text('$' + ThePrice + '.00');
}

$(function () { $('.DoPricing').click(updateTotal); });

Side note: I've tried to use your capitalization style in the above. FYI, it's very unusual to use initially-capped variable names in JavaScript for anything but globals.

10
  • I tried to copy and paste the var AccompPricing you have and replace with mine and it doesn't give a total. Hmm? Commented Jun 13, 2013 at 7:29
  • @PhilMulkins: You can't just grab part of the solution and expect it to work. How you use the entries in AccompPricing changes too. Commented Jun 13, 2013 at 7:31
  • It looks like something that could work, but it just doesn't give a total. #2 needs charged double of OptionsPricing so you are right there but doesn't output a total. Commented Jun 13, 2013 at 7:31
  • You are right T.J., my bad. But it still doesn't work. Is there anything else I need to do to the HTML? Commented Jun 13, 2013 at 7:33
  • I'm a terrible javascripter Btw! But I am pretty good at PHP. I should have learned javascript first thats for sure. Commented Jun 13, 2013 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.