Skip to content

Instantly share code, notes, and snippets.

@davist11
Created December 18, 2012 22:06
Show Gist options
  • Save davist11/4332498 to your computer and use it in GitHub Desktop.
Save davist11/4332498 to your computer and use it in GitHub Desktop.
jQuery List to Select
/**
* @name jQuery List to Select Plugin
* @author Trevor Davis
* @copyright (cc) Trevor Davis (http://www.viget.com)
*
* Licensed under the CC-GNU GPL (http://creativecommons.org/licenses/GPL/2.0/)
*/
;(function($, window, document, undefined) {
var ListSelect = function(elem, options) {
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data('listSelect-options');
this.$links = this.$elem.find('a');
this.selectHTML = '<select>';
};
ListSelect.prototype = {
init: function() {
this.$elem.find('a').each($.proxy(this.buildSelect, this));
this.selectHTML += '</select>';
$(this.selectHTML).insertAfter(this.$elem);
return this;
},
buildSelect: function(index, el) {
var $el = $(el);
this.selectHTML += '<option value="' + $el.attr('href') + '">' + $el.text() + '</option>';
}
};
ListSelect.defaults = ListSelect.prototype.defaults;
$.fn.listSelect = function(options) {
return this.each(function() {
new ListSelect(this, options).init();
});
};
})(jQuery, window , document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment