I am working on with three drop-down menus each of which have three options. Together these three drop-down menus create a system that can be in any one of twenty seven states. For each drop-down, there is option 1, option 2, and all (which is the union of option 1 and 2).
Depending on what state these three drop-down menus are in, I want to display or hide different divs. These divs have classes of this form class="distribution-(select-1)-(select-2)-(select-3)"
I am trying to find a DRY way of coding this up without relying on 27 different conditionals.
Here are the drop-down menus:
<div style = "margin-top:1cm; font-size:125%">
<strong>Clickout Type </strong>
<select class = "clickouts" >
<option value = "all" selected="selected">All</option>
<option value = "clickouts" >Clickouts</option>
<option value = "preferred" >Preferred Clickouts</option>
</select>
<strong>Graph Type </strong>
<select class = "graph-type" >
<option value = "all" selected="selected">All</option>
<option value = "hist" >Histograms</option>
<option value = "percent" >Percent</option>
</select>
<strong>Roll-up Type </strong>
<select class = "roll-up" >
<option value = "all" selected="selected">All</option>
<option value = "aggregate" >Aggregate</option>
<option value = "granular" >Granular</option>
</select>
</div>
Here are some examples of the div classes:
distribution-hist-clickouts-aggregate, distribution-percent-clickouts-aggregate, class="distribution-hist-clickouts-granular", distribution-percent-clickouts-granular
Here is the jquery for the first seven states:
$('.clickouts, .graph-type, .roll-up').change(function(){
var graph_type = $('.graph-type').val();
var roll_up = $('.roll-up').val();
var clickouts = $('.clickouts').val();
if (graph_type == 'all' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="distribution"]').css('display', 'block');
}
if (graph_type == 'all' & roll_up == 'all' & clickouts == 'clickouts'){
$('div[class*="clickout"]').css('display', 'block');
$('div[class*="preferred"]').css('display', 'none');
}
else if (graph_type == 'all' & roll_up == 'all' & clickouts == 'preferred'){
$('div[class*="clickout"]').css('display', 'none');
$('div[class*="preferred"]').css('display', 'block');
}
else if (graph_type == 'hist' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="hist"]').css('display', 'block');
$('div[class*="percent"]').css('display', 'none');
}
else if (graph_type == 'percent' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="hist"]').css('display', 'none');
$('div[class*="percent"]').css('display', 'block');
}
else if (graph_type == 'all' & roll_up == 'aggregate' & clickouts == 'all'){
$('div[class*="aggregate"]').css('display', 'block');
$('div[class*="granular"]').css('display', 'none');
}
else if (graph_type == 'all' & roll_up == 'granular' & clickouts == 'all'){
$('div[class*="aggregate"]').css('display', 'none');
$('div[class*="granular"]').css('display', 'block');
}
});
If you will change your complex class
class="distribution-(select-1)-(select-2)-(select-3)to set of classesclass="distribution (select-1) (select-2) (select-3)you can use the following script:Code: http://jsfiddle.net/9R5zj/9/