Following is a simple bootstrap dropdown menu, with the exception that the toggle element is a text input.
I don't want the dropdown menu to show on click event, rather, I want to show it on user input, so that I can dynamically populate the menu based on that input.
The click event handler below prevents the dropdown from showing before the user has input anything in the input element, and when there is any input, the input handler shows the dropdown menu. However once the user has input something into the element, and dropdown has shown. Afterwards, the any subsequent click on the input element shows the dropdown menu, and the click handler runs but does not prevent the dropdown from showing even if the user clears all input.
Does Bootstrap attach any new events, or am I not understanding events very well? How could I make this work?
$('#exampleFormControlInput1').on('click', e=>{
e.preventDefault();
e.stopPropagation();
console.log('Dropdown prevented');
});
$('#exampleFormControlInput1').on('input', e=>{
$('#exampleFormControlInput1').dropdown('show');
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>
<div class="container">
<div class="dropdown">
<label for="exampleFormControlInput1" class="form-label" id="dropdownMenuButton1">Input with autocomplete</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Search for something" data-toggle="dropdown" aria-expanded="false">
<ul class="dropdown-menu" aria-labelledby="exampleFormControlInput1">
<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>