This jQuery plugin hide or show an option item in a select list.
jQuery.fn.dnkl_option = function(optionSettings) {
/*
PLUGIN: dnkl_option([visible, value, text])
VERS: 2.0
DESC: Hide/show option item in a select list
USAGE: $('#mySelectID').dnkl_option({ visible: false, value: '1' });
*/
var settings = $.extend({ visible: false, value: '', text: '' }, optionSettings || {});
if (settings.value == '' && settings.text == '') {
return $(this);
}
var os = $(this).data('options');
if (typeof (os) === 'undefined') {
$(this).data('options', $('#' + $(this).attr('id') + ' option'));
os = $(this).data('options');
}
var o = null; var index = null;
for (var i = 0; o == null && i < $(os).length; i++) {
if ($(os[i]).val() == settings.value || $(os[i]).text() == settings.text) {
index = i;
o = os[index];
}
}
if (settings.visible) {
var id = '#' + $(this).attr('id');
var visibleOptions = $(id + ' option');
$(id + ' >option').remove();
for (var i = 0; i < $(os).length; i++) {
var o = os[i];
if (index != null && index == i) {
$(this).append(o);
}
else {
for (var j = 0; j < visibleOptions.length; j++) {
if ($(o).val() == $(visibleOptions[j]).val()) {
$(this).append(o);
}
}
}
}
$(id + ' option').first().prop('selected', true);
}
else {
if ($(o).length) {
$(o).remove();
}
}
return $(this);
}