13
loading...
This website collects cookies to deliver better user experience
<input type="search" id="filter" placeholder="Filter" autocomplete="off">
<select id="myOptions"></select>
function getOptions() {
let result = [];
let prefix = ["A","B","C","D","E","F","G","H","I","J","K"];
prefix.forEach(p => {
for(let x=0; x<250; x++) {
result.push(p+x);
}
});
return result;
}
function setOptions(opts) {
let select = document.querySelector('#myOptions');
//set values for drop down
let html = '';
opts.forEach(o => {
html += `<option>${o}</option>`;
});
select.innerHTML = html;
}
let filter = document.querySelector('#filter');
filter.addEventListener('input', () => {
let value = filter.value.trim().toLowerCase();
let options = (getOptions()).filter(f => {
return value === '' || f.toLowerCase().includes(value);
});
setOptions(options);
},false);
setOptions(getOptions());
getOptions
is meant to represent the API call or some other 'real' process. In my case I'm just generating dummy data.setOptions
handles setting the options available to the dropdown. It expects an array of values passed to it. By default this is the full result of getOptions
, but when you type into the filter, it filters the values returned. Here's a demo:multiple
property of the dropdown, providing a bit more visual feedback of the filter being performed. Here's his version.