Is there anything out of the box that bootstrap supports to render a "regular" defacto drop down list select box? That is, where the drop down box is a list of values and if selected populate the contents of the list box?
Something similar to this functionality?
This question is related to
twitter-bootstrap
Another way without using the .form-control is this:
$(".dropdown-menu li a").click(function(){
$(this).parents(".btn-group").find('.btn').html($(this).text() + ' <span class="caret"></span>');
$(this).parents(".btn-group").find('.btn').val($(this).data('value'));
});
$(".dropdown-menu li a").click(function(){_x000D_
$(this).parents(".btn-group").find('.btn').html($(this).text() + ' <span class="caret"></span>');_x000D_
$(this).parents(".btn-group").find('.btn').val($(this).data('value'));_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>_x000D_
_x000D_
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>_x000D_
_x000D_
_x000D_
<div class="btn-group">_x000D_
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">_x000D_
Test <span class="caret"> </span>_x000D_
</button>_x000D_
<ul class="dropdown-menu">_x000D_
<li><a href='#'>test 1</a></li>_x000D_
<li><a href='#'>test 2</a></li>_x000D_
<li><a href='#'>test 3</a></li>_x000D_
</ul>_x000D_
</div>
_x000D_
Another option is to make the Bootstrap dropdown behave like a select using jQuery...
$(".dropdown-menu li a").click(function(){
var selText = $(this).text();
$(this).parents('.btn-group').find('.dropdown-toggle').html(selText+' <span class="caret"></span>');
});
Skelly's nice and easy answer is now outdated with the changes to the dropdown syntax in Bootstap. Instead use this:
$(".dropdown-menu li a").click(function(){
var selText = $(this).text();
$(this).parents('.form-group').find('button[data-toggle="dropdown"]').html(selText+' <span class="caret"></span>');
});
Test: http://jsfiddle.net/brynner/hkwhaqsj/6/
HTML
<div class="input-group-btn select" id="select1">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><span class="selected">One</span> <span class="caret"></span></button>
<ul class="dropdown-menu option" role="menu">
<li id="1"><a href="#">One</a></li>
<li id="2"><a href="#">Two</a></li>
</ul>
</div>
jQuery
$('body').on('click','.option li',function(){
var i = $(this).parents('.select').attr('id');
var v = $(this).children().text();
var o = $(this).attr('id');
$('#'+i+' .selected').attr('id',o);
$('#'+i+' .selected').text(v);
});
CSS
.select button {width:100%; text-align:left;}
.select .caret {position:absolute; right:10px; margin-top:10px;}
.select:last-child>.btn {border-top-left-radius:5px; border-bottom-left-radius:5px;}
.selected {padding-right:10px;}
.option {width:100%;}
I'm currently fighting with dropdowns and I'd like to share my experiences:
There are specific situations where <select>
can't be used and must be 'emulated' with dropdown.
For example if you want to create bootstrap input groups, like Buttons with dropdowns (see http://getbootstrap.com/components/#input-groups-buttons-dropdowns). Unfortunately <select>
is not supported in input groups, it will not be rendered properly.
Or does anybody solved this already? I would be very interested on the solution.
And to make it even more complicated, you can't use so simply $(this).text()
to catch what user selected in dropdown if you're using glypicons or font awesome icons as content for dropdown. For example:
<li id="someId"><a href="#0"><i class="fa fa-minus"></i></a></li>
Because in this case there is no text and if you will add some then it will be also displayed in dropdown element and this is unwanted.
I found two possible solutions:
1)
Use $(this).html()
to get content of the selected <li>
element and then to examine it, but you will get something like <a href="#0"><i class="fa fa-minus"></i></a>
so you need to play with this to extract what you need.
2)
Use $(this).text()
and hide the text in element in hidden span:
<li id="someId"><a href="#0"><i class="fa fa-minus"><span class="hidden">text</span></i></a></li>
.
For me this is simple and elegant solution, you can put any text you need, text will be hidden, and you don't need to do any transformations of $(this).html()
result like in option 1) to get what you need.
I hope it's clear and can help somebody :-)
The Bootstrap3 .form-control
is cool but for those who love or need the drop-down with button and ul option, here is the updated code. I have edited the code by Steve to fix jumping to the hash link and closing the drop-down after selection.
Thanks to Steve, Ben and Skelly!
$(".dropdown-menu li a").click(function () {
var selText = $(this).text();
$(this).closest('div').find('button[data-toggle="dropdown"]').html(selText + ' <span class="caret"></span>');
$(this).closest('.dropdown').removeClass("open");
return false;
});
Ben's code requires the parent div to have the form-group class (I was using btn-group), this is a slightly different version which just searches for the closest div and may even be a bit faster.
$(".dropdown-menu li a").click(function(){
var selText = $(this).text();
$(this).closest('div').find('button[data-toggle="dropdown"]').html(selText + ' <span class="caret"></span>');
});
Another option could be using bootstrap select. On their own words:
A custom select / multiselect for Bootstrap using button dropdown, designed to behave like regular Bootstrap selects.
Source: Stackoverflow.com