Skip to content

Instantly share code, notes, and snippets.

@pitch-gist
Last active Oct 9, 2015
Embed
What would you like to do?
Javascript: Price Switching
// setup our default plans and their pricing options, we only support rounded to the dollar prices
var plans = {
'standard': {
price: 6,
term: 10
},
'premium': {
price: 10,
term: 12
},
'delux': {
price: 12,
term: 15
}
}
// insert these into the page. could hardcode into the page but this method
// allows for easier maintenance of the prices. we loop through all the plans,
// and using the names we insert them into the matching classes on the page.
$.each(plans, function(index, plan){
$("."+index+".price").html(plan.price);
$("."+index+".term").html(plan.term);
});
// if either of our radio options change run this
$('input[name=stories], input[name=bedrooms]').change(function() {
// grab the current selected option's extra price data and convert to integer
var a = $('input[name=stories]:checked').data("extra");
var b = parseInt(a);
var c = $('input[name=bedrooms]:checked').data("extra");
var d = parseInt(c);
// now for every plan type we have add the two options to the base prices
// and then insert them into the page in the correct place as above.
$.each(plans, function(index, plan){
var price = plan.price + b + d;
var term = plan.term + b + d;
$("."+index+".price").html(price);
$("."+index+".term").html(term);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment