Created
June 30, 2013 16:15
-
-
Save skowron-line/5895794 to your computer and use it in GitHub Desktop.
Select to (bootstrap) dropdown..
This file contains 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
(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() + " ") | |
.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