Created
April 28, 2016 17:36
-
-
Save jdcauley/b8bc363be7d0941375ac3816caaf60de to your computer and use it in GitHub Desktop.
turn Selectboxes into bootstrap btn dropdown
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
(function ($, undefined) { | |
$.fn.gfSelect = function () { | |
var Selects = this; | |
Selects.each(function(i){ | |
var GFSelect = $(this), | |
btnHtml = '<div class="btn-group">'; | |
btnHtml += '<button type="button" class="btn btn-default btn-gf-select dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Select One <span class="caret"></span></button>'; | |
btnHtml += '<ul class="dropdown-menu">'; | |
$(this).find('option').each(function(j){ | |
var GFOption = $(this); | |
if(GFOption.text() !== 'Select One'){ | |
btnHtml += '<li><a href="#" class="js-link-val" data-value="' + GFOption.val() + '" data-target-input="' + GFSelect.attr('id') + '">' + GFOption.text() + '</a></li>'; | |
} | |
}); | |
btnHtml += '</ul></div>'; | |
GFSelect.hide(); | |
GFSelect.parent().append(btnHtml); | |
}); | |
$('.js-link-val').click(function(evt){ | |
evt.preventDefault(); | |
var Selected = $(this), | |
id = '#' + Selected.attr('data-target-input'); | |
Selected.parents('.btn-group').find('button').text(Selected.text()); | |
$(id).val(Selected.attr('data-value')); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment