Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle bootstrap button dropdown using another button

Been having some issues getting a Bootstrap button dropdown to toggle (making the list items and dropdown ul element visible) when another button is clicked. Here's what I have so far which doesn't seem to work (v3.3.7). I want the "testing" button to additionally toggle the "test" button dropdown.

<div class="btn-group">
    <a id="test-dropdown-btn" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
        Test <span class="caret"></span>
    </a>
    <ul id="test-dropdown" class="dropdown-menu">
        <li><a href="#">test</a></li>
    </ul>
</div>
<a id="test-btn" class="btn btn-default" onclick="$('#test-dropdown-btn').dropdown('toggle')">testing</a>
like image 858
darrenc Avatar asked Mar 09 '23 18:03

darrenc


1 Answers

You can use the custom attribute data-target to target the button group, so clicking on any element outside will trigger the dropping-down on the targeted button group.

<a id="test-btn" class="btn btn-default" data-toggle="dropdown" data-target=".btn-group">
testing</a>

You should give your button group an id, so that the data-target is more specific.

Demo

like image 153
Hopeless Avatar answered Mar 18 '23 09:03

Hopeless