Reputation: 4178
I have dropdown list in my bootstrap menu.
<li class="dropdown">
<a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">Chose option<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 3</a></li>
<li><a href="#">Option 4</a></li>
</ul>
</li>
I'm trying to get selected option in dropdown as title of dropdown instead of "chose option", as it is now. I have tried several solutions found on this and some other sites, but can't make any of them to work.
Can someone help with this?
Upvotes: 30
Views: 87871
Reputation: 109
In case of multiple dropdowns, this works, not using any ID
<div class="d-flex gap-3 col-md-3">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown"
aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown"
aria-expanded="false">
Dropdown button <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
</div>
<script>
$(".dropdown-menu li a").click(function () {
var selText = $(this).text();
$(this).parents('.dropdown').find('.dropdown-toggle').html(selText + ' <span class="caret"></span>');
});
</script>
Upvotes: 1
Reputation: 21
You can go through this simple example.
<div class="btn-group">
<button class="btn">Please Select From List</button>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
<li><a tabindex="-1" href="#">Item I</a></li>
<li><a tabindex="-1" href="#">Item II</a></li>
<li><a tabindex="-1" href="#">Item III</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Other</a></li>
</ul>
</div>
Add the below script :
$(function(){
$(".dropdown-menu li a").click(function(){
$(".btn:first-child").text($(this).text());
$(".btn:first-child").val($(this).text());
});
});
Also you can try this sample in http://jsbin.com/owuyix/4/edit
Upvotes: 0
Reputation: 33
I found a solution using "onclick"
onclick="$('#your-main-button-name').html('The text you want to show');"
Hope it helps! :)
Upvotes: 0
Reputation: 1
I had the same problem and here is the solution I found.
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" id="options"> <span id="opt">Chose option</span> <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" id="1" href="#" >Option 1</a></li>
<li><a class="dropdown-item" id="2" href="#">Option 2</a></li>
<li><a class="dropdown-item" id="3" href="#" >Option 3</a></li>
<li><a class="dropdown-item" id="4" href="#" >Option 4</a></li>
</ul>
</div>
<script type="text/javascript">
$(function(){
$("#1").click(function () {
$("#opt").text($(this).text());
});
$("#2").click(function () {
$("#opt").text($(this).text());
});
$("#3").click(function () {
$("#opt").text($(this).text());
});
$("#4").click(function () {
$("#opt").text($(this).text());
});
});
</script>
Upvotes: 0
Reputation: 374
Change your html like :
<li class="dropdown">
<button id="options" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" >Chose option<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" >Option 1</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#" >Option 3</a></li>
<li><a href="#" >Option 4</a></li>
</ul>
</li>
and on javascript side do like this way :
<script>
$(document).ready(function(){
$(".dropdown-menu li a").click(function(){
$("#options").text($(this).text());
});
});
</script>
Upvotes: 5
Reputation: 3240
This should work for you:
<div class="dropdown">
<a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">
<span id="selected">Chose option</span><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 3</a></li>
<li><a href="#">Option 4</a></li>
</ul>
</div>
And the script:
$('.dropdown-menu a').click(function(){
$('#selected').text($(this).text());
});
You need to wrap the text 'Choose option' inside <span>
to modify its value on selection.
Here is the working code: http://liveweave.com/U4BpiH
Upvotes: 35