Display Selected Item in Bootstrap Button Dropdown Title / place holder text
Asked Answered
K

2

9

This question has been asked a few times on Stackoverflow - however I still cant get to the bottom of it... plus my query is throwing more dropdowns into play. So I have two dropdowns and a search. I want to select from the dropdowns and the 'selected' to replace the dropdown place holder text. But I also need to keep in mind that after hitting search it will query a database for these selected fields.

            <!-- Search box Start -->
                    <form>
                        <div class="well carousel-search hidden-phone">
                            <div class="btn-group">
                                <a class="btn dropdown-toggle btn-select" data-toggle="dropdown" href="#">Select a Country<span class="caret"></span></a>
                                    <ul class="dropdown-menu">
                                    <li><a href="#">Item I</a></li>
                                    <li><a href="#">Item II</a></li>
                                    <li><a href="#">Item III</a></li>
                                    <li class="divider"></li>
                                    <li><a href="#">Other</a></li>
                                    </ul>
                        </div>

                            <div class="btn-group">
                                <a class="btn dropdown-toggle btn-select2" data-toggle="dropdown" href="#">Select a Region/City<span class="caret"></span></a>
                                    <ul class="dropdown-menu">
                                        <li><a href="#">Item I</a></li>
                                    <li><a href="#">Item II</a></li>
                                    <li><a href="#">Item III</a></li>
                                    <li class="divider"></li>
                                    <li><a href="#">Other</a></li>
                                    </ul>
                            </div>
                            <div class="btn-group">
                                <button type="submit" class="btn btn-primary">Search</button>
                            </div>

                        </div>
                    </form>
            <!-- Search box End -->
Kantor answered 12/6, 2013 at 9:15 Comment(1)
#13437946 this was the similar queryKantor
M
20

You can set the selected dropdown text like this..

$(".dropdown-menu li a").click(function(){
  var selText = $(this).text();
  $(this).parents('.btn-group').find('.dropdown-toggle').html(selText+'<span class="caret"></span>');
});

And then handle the search button click to get the selected values:

$("#btnSearch").click(function(){
    alert($('.btn-select').text()+", "+$('.btn-select2').text());
});

Working demo: http://www.bootply.com/63926

Monarchism answered 12/6, 2013 at 9:48 Comment(0)
L
1

Bootstrap 4 version:

$(".dropdown-item").click(function(){
    var selText = $(this).text();
    $(this).parents('.dropdown').find('.dropdown-toggle').html(selText+'<span class="caret"></span>');
});
Lamprophyre answered 22/11, 2016 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.