Created
December 18, 2012 22:06
-
-
Save davist11/4332498 to your computer and use it in GitHub Desktop.
jQuery List to Select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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