Last active
October 9, 2015 03:38
-
-
Save pitch-gist/3432288 to your computer and use it in GitHub Desktop.
Javascript: Price Switching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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