Usually there is a way to repeat yourself less.
var myObj = {
gender:'',
age:'',
children:'',
income:'',
stage2select:'',
day:'',
spend:'',
categories:'',
product:'',
price:'',
quantity:'',
total:''
};
var dependencies = [ 'gender', 'age', 'children', 'income', 'stage2select',
'day', 'spend', 'categories', 'product', 'price', 'quantity', 'total'];
var i;
for (i = 1; i < dependencies.length; i++) {
if (dependencies[i] != 'stage2select') {
var prev = dependencies[i-1];
var next = dependencies[i];
(function(prev, next) {
$("#" + prev + " li").click(function() {
myObj[prev] = $(this).text();
$('#' + next).show();
update();
});
})(prev, next);
}
}
$("#stage2select :radio").change(function () {
myObj.stage2select = $(this).val();
if ( $(this).val() == 'shopping_patterns') {
$('#day').show();
$('#block3').hide();
$('#block2').show();
}
if ( $(this).val() == 'specific_products') {
$('#product').show();
$('#block2').hide();
$('#block3').show();
}
update();
});
function update() {
var text = "";
$.each(myObj, function(i){
if (this != ''){
if (text.length){
text += " -> ";
}
text += this;
}
});
$("span.jquery_out").text(text);
}
source
share