Trigger Bootstrap Dropdown with JS

Trigger Bootstrap Dropdown with JS

I need to open a Bootstrap dropdown menu via JavaScript. This answer suggests adding and removing the open class, but when I try that, nothing happens. Adding a different class works fine. Is Bootstrap preventing me from doing this?
Working sample on JSFiddle.

$(‘input’).click(function () {


Solution 1:

Your main problem is that you aren’t stopping the click event from propagating to the document. Bootstrap sets an event listener on the document that closes dropdowns.

$('input').on('click', function (e) {

jsFiddle –

Solution 2:

try this…

<div class="dropdown">
    <input type="button" value="Classes" data-toggle="dropdown"/>
    <a data-toggle="dropdown" href="#">Dropdown Trigger</a>
    <div class="dropdown-menu">Dropdown Content</div>


Related:  Bootstrap 4 Center Vertical and Horizontal Alignment