Skip to content

Instantly share code, notes, and snippets.

@skowron-line
Created June 30, 2013 16:15
Show Gist options
  • Save skowron-line/5895794 to your computer and use it in GitHub Desktop.
Save skowron-line/5895794 to your computer and use it in GitHub Desktop.
Select to (bootstrap) dropdown..
(function($){
$.fn.select2Dropdown = function(){
var options = this.find('option'),
selected = this.find('option:selected').length == 0
? options[0]
: this.find('option:selected'),
div = document.createElement('div'),
a = document.createElement('a'),
span = document.createElement('span'),
ul = document.createElement('ul');
$(div).addClass('btn-group');
$(a).addClass('btn dropdown-toggle');
$(span).addClass('caret');
$(ul).addClass('dropdown-menu');
$(ul).append('<li><a href="#">'+ selected.text() +'</a></li>');
options.each(function(index, item){
if($(item).val() == $(selected).val()) return true;
$(ul).append('<li><a href="#">'+ $(item).text() +'</a></li>');
});
$(a).html(selected.text() + "&nbsp;")
.attr('data-toggle','dropdown')
.attr('href', '#')
.append(span);
$(div).append(a);
$(div).append(ul);
this.replaceWith($(div));
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment