Skip to content

Commit 205146b

Browse files
committed
Adding recursion to building up the options in a select box.
1 parent 45d3e0c commit 205146b

File tree

1 file changed

+41
-52
lines changed

1 file changed

+41
-52
lines changed

jquery.selectBox.js

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -182,29 +182,41 @@ if(jQuery) (function($) {
182182
var getOptions = function(select, type) {
183183
var options;
184184

185+
// Private function to handle recursion in the getOptions function.
186+
var _getOptions = function(select, options) {
187+
// Loop through the set in order of element children.
188+
select.children('OPTION, OPTGROUP').each( function() {
189+
// If the element is an option, add it to the list.
190+
if ($(this).is('OPTION')) {
191+
// Check for a value in the option found.
192+
if($(this).length > 0) {
193+
// Create an option form the found element.
194+
generateOptions($(this), options);
195+
}
196+
else {
197+
// No option information found, so add an empty.
198+
options.append('<li>\u00A0</li>');
199+
}
200+
}
201+
else {
202+
// If the element is an option group, add the group and call this function on it.
203+
var optgroup = $('<li class="selectBox-optgroup" />');
204+
optgroup.text($(this).attr('label'));
205+
options.append(optgroup);
206+
options = _getOptions($(this), options);
207+
}
208+
});
209+
// Return the built string.
210+
return options;
211+
}
212+
185213
switch( type ) {
186214

187215
case 'inline':
188216

189-
190217
options = $('<ul class="selectBox-options" />');
191-
192-
if( select.find('OPTGROUP').length ) {
193-
194-
select.find('OPTGROUP').each( function() {
195-
196-
var optgroup = $('<li class="selectBox-optgroup" />');
197-
optgroup.text($(this).attr('label'));
198-
options.append(optgroup);
199-
200-
generateOptions($(this).find('OPTION'), options);
201-
202-
});
203-
204-
} else {
205-
generateOptions(select.find('OPTION'), options);
206-
}
207-
218+
options = _getOptions(select, options);
219+
208220
options
209221
.find('A')
210222
.bind('mouseover.selectBox', function(event) {
@@ -228,27 +240,7 @@ if(jQuery) (function($) {
228240

229241
case 'dropdown':
230242
options = $('<ul class="selectBox-dropdown-menu selectBox-options" />');
231-
232-
if( select.find('OPTGROUP').length ) {
233-
234-
select.find('OPTGROUP').each( function() {
235-
236-
var optgroup = $('<li class="selectBox-optgroup" />');
237-
optgroup.text($(this).attr('label'));
238-
options.append(optgroup);
239-
generateOptions($(this).find('OPTION'), options);
240-
241-
});
242-
243-
} else {
244-
245-
if( select.find('OPTION').length > 0 ) {
246-
generateOptions(select.find('OPTION'), options);
247-
} else {
248-
options.append('<li>\u00A0</li>');
249-
}
250-
251-
}
243+
options = _getOptions(select, options);
252244

253245
options
254246
.data('selectBox-select', select)
@@ -816,19 +808,16 @@ if(jQuery) (function($) {
816808
});
817809
};
818810

819-
var generateOptions = function(originalOptions, options){
820-
originalOptions.each(function(){
821-
var self = $(this);
822-
var li = $('<li />'),
823-
a = $('<a />');
824-
li.addClass( self.attr('class') );
825-
li.data( self.data() );
826-
a.attr('rel', self.val()).text( self.text() );
827-
li.append(a);
828-
if( self.attr('disabled') ) li.addClass('selectBox-disabled');
829-
if( self.attr('selected') ) li.addClass('selectBox-selected');
830-
options.append(li);
831-
});
811+
var generateOptions = function(self, options){
812+
var li = $('<li />'),
813+
a = $('<a />');
814+
li.addClass( self.attr('class') );
815+
li.data( self.data() );
816+
a.attr('rel', self.val()).text( self.text() );
817+
li.append(a);
818+
if( self.attr('disabled') ) li.addClass('selectBox-disabled');
819+
if( self.attr('selected') ) li.addClass('selectBox-selected');
820+
options.append(li);
832821
};
833822

834823
//

0 commit comments

Comments
 (0)