Display currently selected option in dropdown? - html

I have the following code:
<form class="search-bar" data-example-id="search-bar">
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with segmented button dropdown">
<div class="input-group-btn">
<button type="button" class="btn btn-default">Search</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
</ul>
</div>
</div><!-- /.input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</form>
What I am trying to do is get it functioning so that if a user clicks the dropdown and selections Option 1, Option 1 will be displayed in the dropdown rather than the little caret glyph.
This is using Bootstrap, just to clarify.

You use Bootstrap Select instead of Bootstrap dropdown:
http://silviomoreto.github.io/bootstrap-select/

You need to add some JS magic. Here's in jQuery flavour
jQuery
$(".dropdown-menu li a").click(function(){
$(this).parents(".input-group-btn").find('.dropdown-toggle').text($(this).text());
$(this).parents(".input-group-btn").find('.dropdown-toggle').val($(this).text());
});
Your HTML
<form class="search-bar" data-example-id="search-bar">
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with segmented button dropdown">
<div class="input-group-btn">
<button type="button" class="btn btn-default">Search</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
</ul>
</div>
</div><!-- /.input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
</form>
And here's a working demo.
EDIT: You can stick this in the head of your page like so
<script type="text/javascript">
$(document).ready(function () {
$(".dropdown-menu li a").click(function () {
$(this).parents(".input-group-btn").find('.dropdown-toggle').text($(this).text());
$(this).parents(".input-group-btn").find('.dropdown-toggle').val($(this).text());
});
});
</script>

Related

How to place bootstrap dropdown menu button in page-header on the right site?

The case seems idiotically simple, but I can not deal with setting the elements.
What I have:
or:
What I need:
Code:
JSFiddle
<div class="container">
<div class="page-header">
<h2>Header</h2>
<div class="btn-group <!-- pull-right -->">
<a class="btn btn-success" href="/Scope/New">Create</a>
<div class="btn-group">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu pre-scrollable" role="menu">
<li class="dropdown-header">Create as copy: </li>
<li>Test 1</li>
<li>Test 45</li>
<li>Lorem ipsum</li>
</ul>
</div>
</div>
</div>
</div>
.page-header {
padding-bottom: 9px;
margin: 40px 0 20px;
border-bottom: 1px solid #eee;
display: flex;
align-items: center;
justify-content: space-between;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="page-header">
<h2>Header</h2>
<div class="btn-group">
<a class="btn btn-success" href="/Scope/New">Create</a>
<div class="btn-group">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu pre-scrollable" role="menu">
<li class="dropdown-header">Create as copy: </li>
<li>Test 1</li>
<li>Test 45</li>
<li>Lorem ipsum</li>
</ul>
</div>
</div>
</div>
</div>
thats all , u can use flex.
Add clearfix in page-header and also add display: inline; to h2.
This will fix your issue. (currently h2 added inline, you can place it in your CSS file)
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="page-header clearfix
">
<h2 style="display: inline;">Header</h2>
<div class="btn-group <!-- pull-right -->">
<a class="btn btn-success" href="/Scope/New">Create</a>
<div class="btn-group">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu pre-scrollable" role="menu">
<li class="dropdown-header">Create as copy: </li>
<li>Test 1</li>
<li>Test 45</li>
<li>Lorem ipsum</li>
</ul>
</div>
</div>
</div>
</div>
add clearfix to page-header
<div class="page-header clearfix">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="page-header clearfix
">
<h2>Header</h2>
<div class="btn-group <!-- pull-right -->">
<a class="btn btn-success" href="/Scope/New">Create</a>
<div class="btn-group">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu pre-scrollable" role="menu">
<li class="dropdown-header">Create as copy: </li>
<li>Test 1</li>
<li>Test 45</li>
<li>Lorem ipsum</li>
</ul>
</div>
</div>
</div>
</div>

Nested row fluid grid not working

I have 5 dropdrowns and a column on right corner arranged horizontally in a same row. In the next row I would like to have few more dropdowns but I am not able to position in properly as the column on the right is width 500px;
Example:
--------- --------- --------- --------- ---------
dropdown1 dropdown2 dropdown3 dropdown4 dropdown5 column
--------- --------- --------- --------- --------- ---------
| Ads |
| |
>I want to have a drop down here | |
--------- --------- --------- --------- | |
dropdown1 dropdown2 dropdown3 dropdown4 | |
| |
| |
| |
| |
| |
| |
| |
| |
---------
But its getting positioned here >>
Here is what I have tried:
<div class="starter-template">
<div class="row">
<div class="col-sm-12">
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Search Job</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Search Employer</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Search Referee</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Job fair & Events</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">My RTH</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2 ad-block-right text-center" style="width:210px; height: 500px; border: thin black; border-style: dotted">
Advertisements
</div>
</div>
<!--############# NESTED ROW NOT WORKING ###########-->
<div class="row">
<div class="starter-template">
<div class="col-xs-6 col-sm-6">
<div class="">Search all jobs</div>
</div>
</div>
</div>
</div>
</div>
</div>
Your structure is not right. First use row, then inside row separate with col-sm-8 and col-sm-4 (here ads column). And inside col-sm-8 use row to separate rows of dropdrowns
<div class="starter-template">
<div class="row">
<div class="col-sm-8">
<div class="row">
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Search Job</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Search Employer</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Search Referee</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">Job fair & Events</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
<div class="col-sm-2">
<div class="btn-group btn-block">
<button class="col-sm-10 btn btn-sm btn-default">My RTH</button>
<button class="col-sm-2 btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu btn-block">
<li>Reason 1</li>
<li>Reason 2</li>
<li>Reason 3</li>
<li>Reason 4</li>
</ul>
</div>
</div>
</div>
<!--############# NESTED ROW NOT WORKING ###########-->
<div class="row">
<div class="starter-template">
<div class="col-xs-6 col-sm-6">
<div class="">Search all jobs</div>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="col-sm-2 ad-block-right text-center" style="width:210px; height: 500px; border: thin black; border-style: dotted">
Advertisements
</div>
</div>
</div>
</div>
</div>
This is the basic skeleton you need:
<div class=row>
<div class=col-md-10>
<div class="row">
<div class=col-md-2>
dropdown1
</div>
<div class=col-md-2>
dropdown2
</div>
<div class=col-md-2>
dropdown3
</div>
<div class=col-md-2>
dropdown4
</div>
<div class=col-md-2>
dropdown5
</div>
</div>
<div class="row">
<div class=col-md-2>
dropdown1
</div>
<div class=col-md-2>
dropdown2
</div>
<div class=col-md-2>
dropdown3
</div>
<div class=col-md-2>
dropdown4
</div>
</div>
</div>
<div class=col-md-2>
// your ads here
</div>
</div>

Bootstrap breaking point on nav bar

I have started using bootstrap recently so I am still learning. My problem is on the navigation bar i have created. I have all the contents pulling right but the search bar and tabs are breaking line. I have tried a few things like setting break points to xs and so on.
here is what it looks like.
enter image description here
<div class="container-fluid">
<div class="row-fluid">
</div>
<div class="row-xs pull-right" >
<div class="col-xs">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
</ul>
</div>
<div class"col-xs">
<form class="navbar-form " role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search"></div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</nav>
</div>
You're HTML is missing numerous elements row-xs, col-xs, and row-fluid are not classes in Bootstrap3 and you have some syntax errors also.
I would suggest starting out with a standard navbar class and customizing it from there but if you have or want to use columns and row see the Docs on the right way to use these in general.
See working examples of a navbar and a variation of your code in the Snippet.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav" aria-expanded="false"> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-nav">
<ul class="nav navbar-nav navbar-right">
<li class="active">Home
</li>
<li>Page 1
</li>
<li>Page 2
</li>
<li>Page 3
</li>
</ul>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..."> <span class="input-group-btn">
<button class="btn btn-default" type="button">Search</button>
</span>
</div>
</div>
</form>
</div>
</div>
</nav>
<hr>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<ul class="nav navbar-nav navbar-right">
<li class="active">Home
</li>
<li>Page 1
</li>
<li>Page 2
</li>
<li>Page 3
</li>
</ul>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav" aria-expanded="false"> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
and what does "navbar-toggle collapsed" do.
Thanks

Add section in a nav bar

In bootstrap 3, i have a nav bar, with an form bar.
I try to add another section just to the right of the search button.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-12">
<nav role="navigation" class="navbar navbar-default pull-right">
<div class="container-fluid">
<form class="navbar-form" role="search">
<div class="checkbox">
<label>
<input type="checkbox" id="inactiveLodger">Locataire inactif</label>
</div>
<div class="input-group">
<input type="text" class="form-control" placeholder="Rechercher" name="srch-term" id="srch-term">
<div class="input-group-btn">
<button id="lodgerSearch" type="button" class="btn btn-default"><i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown"> Paul Smith<b class="caret"></b>
<ul class="dropdown-menu">
<li><i class="glyphicon glyphicon-lock"></i> Changer mot de passe
</li>
<li id="logout"><i class="glyphicon glyphicon-log-out"></i>Déconnexion
</li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
Righ now, the other section is below, like you can see on the image.
If you want to use float:right that is what navbar-right does, simply invert order of form and dropdown. right floated element must be placed BEFORE others in order to make others floats around them.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-12">
<nav role="navigation" class="navbar navbar-default pull-right">
<div class="container-fluid">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown"> Paul Smith<b class="caret"></b>
<ul class="dropdown-menu">
<li><i class="glyphicon glyphicon-lock"></i> Changer mot de passe
</li>
<li id="logout"><i class="glyphicon glyphicon-log-out"></i>Déconnexion
</li>
</ul>
</li>
</ul>
<form class="navbar-form" role="search">
<div class="checkbox">
<label>
<input type="checkbox" id="inactiveLodger">Locataire inactif</label>
</div>
<div class="input-group">
<input type="text" class="form-control" placeholder="Rechercher" name="srch-term" id="srch-term">
<div class="input-group-btn">
<button id="lodgerSearch" type="button" class="btn btn-default"><i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>
</div>
</nav>
</div>
Unless you're applying other changes to the navbar, you shouldn't need the extra classes you've applied (col-sm-12/pull-right/etc). Just use the default classes and structure and but reorder your right floated navigation elements so they're in the order you want.
See example.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button> <a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Link <span class="sr-only">(current)</span>
</li>
<li>Link
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown"> Paul Smith <span class="caret"></span>
<ul class="dropdown-menu">
<li><i class="glyphicon glyphicon-lock"></i> Changer mot de passe
</li>
<li><i class="glyphicon glyphicon-log-out"></i> Déconnexion
</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="inactiveLodger">Locataire inactif</label>
</div>
<div class="input-group">
<input type="text" class="form-control" placeholder="Rechercher" name="srch-term" id="srch-term">
<div class="input-group-btn">
<button id="lodgerSearch" type="button" class="btn btn-default"><i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</div>
</form>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>

Bootstrap 3: dropdown pulled right

I need to have a dropdown pulled right. In my code below, the dropdown is perfectly pulled right, but clicking the dropdown is showing the options way off.
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<button class="btn btn-info">Button</button>
</div>
<div class="col-md-8">
<form class="form-inline pull-right" role="form">
<div class="form-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Option A</li>
<li>Option B</li>
</ul>
</div> <input data-type="text" class="form-control" type="text" placeholder="Search...">
</form>
</div>
</div>
</div>
Fiddle
How can I align the options with the dropdown?
Thanks
Add a btn-group class after form-group
<div class="form-group btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">Dropdown
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Option A</li>
<li>Option B</li>
</ul>
</div>
Here is Fiddle