
Release candidate (RC)
This component is in the Release Candidate phase and its API is considered stable. Minor adjustments may still occur before the final release.
Bootstrap 5 Autocomplete
Autocomplete
Create intelligent, responsive input fields using the Bootstrap Autocomplete plugin. This component enhances user input with dynamic dropdown suggestions, flexible data providers, and callback support.
🤖 Looking for the LLM-optimized version?View llm.md
Overview
TheBootstrap Autocomplete Dropdown component is a powerful autocomplete dropdown plugin that enhances form control usability by suggesting completions as the user types. Whether you’re integrating with local JavaScript arrays or fetching live server responses via AJAX, this plugin enables rich, dynamic user interactions.
Basic example
A straightforward demonstration of how to implement a basic Bootstrap Autocomplete input field, highlighting essential attributes and configurations.
<divdata-coreui-toggle="autocomplete"data-coreui-cleaner="true"data-coreui-highlight-options-on-search="true"data-coreui-indicator="true"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Search technologies..."data-coreui-search="global"data-coreui-search-no-results-label="No results found"data-coreui-show-hints="true"data-coreui-value="React.js"></div>
Data source
Learn how to populate the autocomplete component with data from various sources, including arrays and objects, for dynamic content integration.
Array data
To dynamically populate an autocomplete input with suggestions from an array, use JavaScript to configure the component with predefined options.
<divid="myAutoComplete"></div>
We use the following JavaScript to set up our autocomplete:
constmyAutoComplete=document.getElementById('myAutoComplete')if(myAutoComplete){newcoreui.Autocomplete(myAutoComplete,{name:'autocomplete',options:['Angular','Bootstrap',{label:'React.js',disabled:true},'Vue.js',{label:'backend',options:['Django','Laravel','Node.js']}],value:'Laravel'})}
Object data with groups
You can organize suggestions into groups using optgroups for better categorization and user experience.
<divid="myAutoCompleteGrouped"></div>
constmyAutoCompleteGrouped=document.getElementById('myAutoCompleteGrouped')if(myAutoCompleteGrouped){newcoreui.Autocomplete(myAutoCompleteGrouped,{name:'autocomplete-grouped',options:['Angular',{label:'Bootstrap',selected:true},{label:'React.js',disabled:true},'Vue.js',{label:'backend',options:['Django','Laravel','Node.js']}]})}
Options with values
You can use values in your options array. This is particularly useful when working with database IDs, product codes, or any numeric identifiers. Note that the component internally converts number values to strings for consistency and DOM compatibility.
<divid="myAutoCompleteValues"></div>
constmyAutoCompleteValues=document.getElementById('myAutoCompleteValues')if(myAutoCompleteValues){newcoreui.Autocomplete(myAutoCompleteValues,{cleaner:true,indicator:true,name:'autocomplete-option-values',options:[{label:'Product A',value:1},{label:'Product B',value:2},{label:'Product C',value:3.5},{label:'Product D',value:42},{label:'Categories',options:[{label:'Electronics',value:100},{label:'Books',value:200},{label:'Clothing',value:300}]}],placeholder:'Select product by ID...',search:'global',value:100})// Log the selected value to demonstrate that numbers are converted to stringsmyAutoCompleteValues.addEventListener('changed.coreui.autocomplete',event=>{// eslint-disable-next-line no-consoleconsole.log('Selected value:',event.value.value)// eslint-disable-next-line no-consoleconsole.log('Selected label:',event.value.label)})}
Important: While you can pass number values in your options, the component internally converts all values to strings usingString(value)
. When handling selection events, remember thatevent.value.value
will always be a string representation of your original number.
For example:
- Input:
{ label: 'Product A', value: 42 }
- Internal storage:
{ label: 'Product A', value: '42' }
- Event output:
event.value.value === '42'
(string)
External data
You can configure CoreUI’s AutoComplete component to fetch and display options dynamically from an external API. This is useful when you need to autocomplete data that changes frequently or is too large to preload.
This example shows how to connect the AutoComplete to a remote API to search users by first name.
<divid="myAutoCompleteExternalData"></div>
To fetch data from an external source, you must set the search option to'external'
or['external', 'global']
.This disables internal filtering and allows you to provide custom data (e.g., from an API) based on user input.
constmyAutoCompleteExternalData=document.getElementById('myAutoCompleteExternalData')if(myAutoCompleteExternalData){constgetUsers=async(name='')=>{try{constresponse=awaitfetch(`https://apitest.coreui.io/demos/users?first_name=${name}&limit=10`)constusers=awaitresponse.json()returnusers.records.map(user=>({value:user.id,label:user.first_name}))}catch(error){// eslint-disable-next-line no-consoleconsole.error('Error fetching users:',error)}}constautocomplete=newcoreui.Autocomplete(myAutoCompleteExternalData,{cleaner:true,highlightOptionsOnSearch:true,name:'autocomplete-external',options:[],placeholder:'Search names...',search:['external','global'],// 🔴 'external' is required for external searchshowHints:true})letlastQuery=nullletdebounceTimer=nullmyAutoCompleteExternalData.addEventListener('show.coreui.autocomplete',async()=>{constusers=awaitgetUsers()autocomplete.update({options:users})})myAutoCompleteExternalData.addEventListener('input.coreui.autocomplete',event=>{constquery=event.valueif(query===lastQuery){return}lastQuery=queryclearTimeout(debounceTimer)debounceTimer=setTimeout(async()=>{constusers=awaitgetUsers(query)autocomplete.update({options:users})},200)})}
Search functionality
Configure the search behavior within the component. Thedata-coreui-search
option determines how the search input operates and provides different search modes.
By default, search applies only while the input field is focused:
<divdata-coreui-toggle="autocomplete"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Search technologies..."></div>
Global search
Enable global search functionality that works across the entire component, regardless of focus position:
<divdata-coreui-toggle="autocomplete"data-coreui-search="global"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Global search enabled..."></div>
External search
Use external search mode to fetch data dynamically from a server or API. This disables internal filtering and delegates the search logic entirely to your application via #"0"><divid="myAutoCompleteExternalDataExample"></div>
constautocomplete=newcoreui.AutoComplete(myAutoCompleteExternalDataExample,{search:'external',// Required for remote data sourcesoptions:[],placeholder:'Search technologies...'})myAutoCompleteExternalDataExample.addEventListener('input.coreui.autocomplete',asyncevent=>{constquery=event.valueconstresponse=awaitfetch(`https://example.com/api?q=${query}`)constresults=awaitresponse.json()autocomplete.update({options:results.map(item=>({value:item.id,label:item.name}))})})
Whensearch: 'external'
is used, the component no longer filters options internally — you must handle all data fetching and filtering logic manually.
You can combine it with'global'
(e.g. search:['external', 'global']
) to allow search input even when focus is not in the text field.
Restricted selection
Restrict users to only select from predefined options by settingdata-coreui-allow-only-defined-options="true"
:
<divdata-coreui-toggle="autocomplete"data-coreui-allow-only-defined-options="true"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Only predefined options allowed..."></div>
Hints and completion
Enable intelligent hints and auto-completion features to improve user experience.
Show hints
Display completion hints as users type:
<divdata-coreui-toggle="autocomplete"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Type to see hints..."data-coreui-show-hints="true"></div>
Highlight matching text
Highlight matching portions of suggestions during search:
<divdata-coreui-toggle="autocomplete"data-coreui-highlight-options-on-search="true"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Search with highlighting..."></div>
Validation states
Apply validation styling to indicate input validity.
<divdata-coreui-toggle="autocomplete"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Valid autocomplete..."data-coreui-valid="true"></div>
<divdata-coreui-toggle="autocomplete"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Invalid autocomplete..."data-coreui-invalid="true"></div>
Disabled state
Add thedata-coreui-disabled="true"
attribute to disable the component:
<divdata-coreui-toggle="autocomplete"data-coreui-disabled="true"data-coreui-options=""data-coreui-placeholder="Disabled autocomplete..."></div>
Sizing
You may also choose from small and large auto completes to match our similarly sized text inputs.
<divclass="autocomplete-lg"data-coreui-cleaner="true"data-coreui-highlight-options-on-search="true"data-coreui-indicator="true"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Large autocomplete..."data-coreui-search="global"data-coreui-show-hints="true"data-coreui-toggle="autocomplete"></div><divdata-coreui-cleaner="true"data-coreui-highlight-options-on-search="true"data-coreui-indicator="true"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Normal autocomplete..."data-coreui-search="global"data-coreui-show-hints="true"data-coreui-toggle="autocomplete"></div><divclass="autocomplete-sm"data-coreui-cleaner="true"data-coreui-highlight-options-on-search="true"data-coreui-indicator="true"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="Small autocomplete..."data-coreui-search="global"data-coreui-show-hints="true"data-coreui-toggle="autocomplete"></div>
Cleaner functionality
Enable a cleaner button to quickly clear input element:
<divdata-coreui-toggle="autocomplete"data-coreui-cleaner="true"data-coreui-options="Angular, Bootstrap, React.js, Vue.js"data-coreui-placeholder="With cleaner button..."></div>
Custom templates
The CoreUI Bootstrap Autocomplete Component provides the flexibility to personalize options and group labels by utilizing custom templates. You can easily customize the options using theoptionsTemplate
, and for groups, you can useoptionsGroupsTemplate
, as demonstrated in the examples below:
<divclass="row"><divclass="col-md-6"><divid="myAutocompleteCountries"></div></div><divclass="col-md-6"><divid="myAutocompleteCountriesAndCities"></div></div></div>
We use the following JavaScript to set up our autocomplete:
constmyAutocompleteCountries=document.getElementById('myAutocompleteCountries')constmyAutocompleteCountriesAndCities=document.getElementById('myAutocompleteCountriesAndCities')if(myAutocompleteCountries&&myAutocompleteCountriesAndCities){constcountries=[{value:'pl',label:'Poland',flag:'🇵🇱'},{value:'de',label:'Germany',flag:'🇩🇪'},{value:'us',label:'United States',flag:'🇺🇸'},{value:'es',label:'Spain',flag:'🇪🇸'},{value:'gb',label:'United Kingdom',flag:'🇬🇧'}]constcities=[{label:'United States',code:'us',flag:'🇺🇸',options:[{value:'au',label:'Austin'},{value:'ch',label:'Chicago'},{value:'la',label:'Los Angeles'},{value:'ny',label:'New York'},{value:'sa',label:'San Jose'}]},{label:'United Kingdom',code:'gb',flag:'🇬🇧',options:[{value:'li',label:'Liverpool'},{value:'lo',label:'London'},{value:'ma',label:'Manchester'}]}]newcoreui.Autocomplete(myAutocompleteCountries,{cleaner:true,indicator:true,options:countries,optionsTemplate(option){return`<div class="d-flex align-items-center gap-2"><span class="fs-5">${option.flag}</span><span>${option.label}</span></div>`},placeholder:'Select country',showHints:true,search:'global'})newcoreui.Autocomplete(myAutocompleteCountriesAndCities,{cleaner:true,indicator:true,options:cities,optionsGroupsTemplate(optionGroup){return`<div class="d-flex align-items-center gap-2"><span class="text-body fs-5">${optionGroup.flag}</span><span>${optionGroup.label}</span></div>`},placeholder:'Select city',showHints:true,search:'global'})}
Usage
Heads up! In our documentation, all examples showstandard CoreUI implementation. If you are using aBootstrap-compatible version of CoreUI, remember to use the following changes:
- In the constructor, please usebootstrap instead ofcoreui. For example,
new bootstrap.Alert(...)
instead ofnew coreui.Alert(...)
- In events, please usebs instead ofcoreui, for example
close.bs.alert
instead ofclose.coreui.alert
- In data attributes, please usebs instead ofcoreui. For example,
data-bs-toggle="..."
instead ofdata-coreui-toggle="..."
Via data attributes
Addautocomplete
class to a container element with an input field:
<divdata-coreui-toggle="autocomplete"data-coreui-search="true"></div>
Via JavaScript
Initialize the autocomplete component via #"0"><divdata-coreui-toggle="autocomplete"></div>