How can I render a list select box (dropdown) with bootstrap?
Twitter BootstrapTwitter Bootstrap Problem Overview
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?
Twitter Bootstrap Solutions
Solution 1 - Twitter Bootstrap
Bootstrap 3 uses the .form-control
class to style form components.
<select class="form-control">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
Solution 2 - Twitter Bootstrap
You can make a bootstrap select using the Dropdown component...
Bootstrap 5 (update 2021)
<a class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" href="#" id="selectA">Select<span class="caret"></span></a>
<ul class="dropdown-menu">
<li class="dropdown-item"><a href="#">Item I</a></li>
<li class="dropdown-item"><a href="#">Item II</a></li>
<li class="dropdown-item"><a href="#">Item III</a></li>
<li class="dropdown-item"><a href="#">Item IV</a></li>
</ul>
let links = document.querySelectorAll('.dropdown-item')
links.forEach(element => element.addEventListener("click", function () {
let text = element.innerText
document.getElementById('selectA').innerText = text
}))
Original answer (Bootstrap 3)
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>');
});
Solution 3 - Twitter Bootstrap
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%;}
Solution 4 - Twitter Bootstrap
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.
Solution 5 - Twitter Bootstrap
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>');
});
Solution 6 - 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(){
$(this).parents(".btn-group").find('.btn').html($(this).text() + ' <span class="caret"></span>');
$(this).parents(".btn-group").find('.btn').val($(this).data('value'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="btn-group">
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Test <span class="caret"> </span>
</button>
<ul class="dropdown-menu">
<li><a href='#'>test 1</a></li>
<li><a href='#'>test 2</a></li>
<li><a href='#'>test 3</a></li>
</ul>
</div>
Solution 7 - Twitter Bootstrap
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>');
});
Solution 8 - Twitter Bootstrap
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;
});
Solution 9 - Twitter Bootstrap
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:
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.
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 :-)