jquery autocomplete bootstrap modal
To implement jQuery autocomplete in a Bootstrap modal, you can follow these steps:
- Include the required dependencies: Make sure you have included jQuery, Bootstrap, and jQuery UI libraries in your HTML file. You can include them using
<script>
tags.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
- Create an input field: Add an input field to your modal where the autocomplete suggestions will appear.
<input type="text" id="autocompleteInput" class="form-control">
- Initialize the autocomplete: In your JavaScript code, initialize the autocomplete on the input field using the
autocomplete()
function from jQuery UI. Set thesource
option to the data source for autocomplete suggestions.
$(document).ready(function() {
$("#autocompleteInput").autocomplete({
source: ["Option 1", "Option 2", "Option 3"] // Replace with your data source
});
});
- Open the modal: When you open the Bootstrap modal, make sure to call the
autocomplete("search")
method to show the autocomplete suggestions.
$("#myModal").on("shown.bs.modal", function() {
$("#autocompleteInput").autocomplete("search", "");
});
That's it! Now you should have a Bootstrap modal with jQuery autocomplete functionality. Customize the data source and other options according to your requirements.
Note: The example provided assumes that you have a Bootstrap modal with the id myModal
and an input field with the id autocompleteInput
. Make sure to adjust the ids and data source according to your specific HTML structure and data requirements.