There is a related question with great response, but the approach and my actual implementation requires different strategy.
Please view the demo sample I have done as my homework: http://jsfiddle.net/k8fNb/
I have two home works:
- Make select options hidden and visible based on data-filter: TODO
- Make the select option selected based on radio value: DONE
When you click a radio, the select value should be the same as radio value (#2), and done, but this should also filter the visibility of select options based on radios' data-filter. Data filter ["a","b","c"]
on #filters
should make the options with values a, b, c visible, and hide the rest. My issue is how to do with #1.
HTML:
<div id="filters">
<input type="radio" name="filter" value="a" data-filter="["a","b","c"]"/><label>a </label>
<input type="radio" name="filter" value="b" data-filter="["a","b","c"]"/><label>b </label>
<input type="radio" name="filter" value="c" data-filter="["a","b","c"]"/><label>c </label>
<input type="radio" name="filter" value="1" data-filter="["1","2","3"]"/><label>1 </label>
<input type="radio" name="filter" value="2" data-filter="["1","2","3"]"/><label>2 </label>
<input type="radio" name="filter" value="3" data-filter="["1","2","3"]"/><label>3 </label>
</div>
<select id="options">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
jQuery:
$(document).ready(function(){
$('#filters input').each(function () {
var v = $(this).val();
$(this).click(function () {
$('#options option').filter(function(){
// 1. Make select options hidden or visible based on data-filter.
// ? TODO ....
// 2. FIXED. Change select value to use radio value
return $(this).val() == v;
}).prop("selected", true);
});
});
});
Any help is much appreciated.