I have a select tag with multiple options. I have a search box for filtering, which hides or shows options depending on what text you type. I want to drop down options the same way it works when I click on it, but I want to do it programmatically. I hope someone understands what I am trying to say.
with other words, while the user is typing something in the search textbox, I want my dropdown element to drop down, the user will be able to see search results while typing...
I have found some examples with <ul>
and <li>
, but I want with <select>
and <option>
.
HTML
<select id="select_street">
<option value="1">Street 1</option>
<option value="2">Street 2</option>
<option value="3">Street 3</option>
<option value="4">Street 4</option>
</select>
<input type="text" id="search_filter" />
Javascript/jQuery
$("#search_filter").on('keyup', function (e) {
$("#select_street option").each(function () {
if ($(this).html().toLowerCase().includes($.trim($('#search_filter').val()).toLowerCase())) {
$(this).show();
} else {
$(this).hide();
}
});
});
Thanks!