JQuery SELECT filter options based on Radio PRE - selected on load
So, yesterday I posted a question ( Link ) and got the answer I need, but for life I cannot achieve this if the value of the selected is on the load on the body. it only works if the user clicks the radio and then filters the options. Help me please!!!
Function
jQuery.fn.filterOn = function(radio, values) {
return this.each(function() {
var select = this;
var options = [];
$(select).find('option').each(function() {
options.push({value: $(this).val(), text: $(this).text()});
});
$(select).data('options', options);
$(radio).click(function() {
var options = $(select).empty().data('options');
var haystack = values[$(this).attr('id')];
$.each(options, function(i) {
var option = options[i];
if($.inArray(option.value, haystack) !== -1) {
$(select).append(
$('<option>').text(option.text).val(option.value)
);
}
});
});
});
};
How to call
$(function() {
$('#theOptions').filterOn('input:radio[name=abc123]', {
'abc': ['a','b','c'],
'123': ['1','2','3']
});
});
Thanks in advance
BTW Here's a DEMO in action
0
a source to share