kstullich kstullich - 12 days ago 11
CSS Question

Boostrap 'Buttons with Dropdowns'

I have this dropdown menu from Boostrap. I have different categories that a user could choose from. When they click the 'Department' button and select a department, I would like the 'Department' button to change to whatever the user selected. I have attached the HTML for the dropdown.

<form class="navbar-form navbar-left" action="{{ url_for('my_view.product_search') }}" method="GET">
<div class="form-group">
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Department <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Laptops</a></li>
<li><a href="#">Cameras</a></li>
<li><a href="#">Gaming</a></li>
<li><a href="#">Networking</a></li>
<li><a href="#">Tools</a></li>
<li><a href="#">Headphones</a></li>
<li><a href="#">Speakers</a></li>
<li><a href="#">Miscellaneous</a></li>
<li><a href="#">Blu-Ray</a></li>
</ul>
</div><!-- /Dropdown Button -->
<input type="text" name="name" class="form-control" placeholder="Search Products...">
</div><!-- /input-group -->
<button type="submit" class="btn btn-default">Submit</button>
</div><!-- /.col-lg-6 -->
</div>
</form><!-- END SEARCH FUNCTION -->


EDIT
Here is a link to what the dropdown looks like

Answer

You need to use javascript. I added a click event to the links within the <li> tag, and a <span> within your button to allow for simple jQuery selection. The URLs here are relative to my local testing environment.

This is a working example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>StackOverflow Testing...</title>
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
</head>
<body>

<form action method="post">
    <div class="row">
        <div class="col-lg-6">
            <div class="input-group">
                <div class="input-group-btn">
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span id="button_text">Department</span>&nbsp;<span class="caret"></span></button>
                    <ul class="dropdown-menu">
                        <li><a class="select_option" href="#">Laptops</a></li>
                        <li><a class="select_option" href="#">Cameras</a></li>
                        <li><a class="select_option" href="#">Gaming</a></li>
                        <li><a class="select_option" href="#">Networking</a></li>
                        <li><a class="select_option" href="#">Tools</a></li>
                        <li><a class="select_option" href="#">Headphones</a></li>
                        <li><a class="select_option" href="#">Speakers</a></li>
                        <li><a class="select_option" href="#">Miscellaneous</a></li>
                        <li><a class="select_option" href="#">Blu-Ray</a></li>
                    </ul>
                </div><!-- /Dropdown Button -->
            <input type="text" name="name" class="form-control" placeholder="Search Products...">
            </div><!-- /input-group -->
        <button type="submit" class="btn btn-default">Submit</button>
        </div><!-- /.col-lg-6 -->
    </div>
</form>

<script charset="utf-8" type="text/javascript" src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<script charset="utf-8" type="text/javascript" src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script charset="utf-8" type="text/javascript">
    $('a.select_option').bind('click', function() {
        var selected = $(this).text();
        $('#button_text').text(selected);
    });
</script>
</body>
</html>