'Searchable select option
Is there anyway to make select option as selectable/auto complete/searchable? unfortunately i cannot change the form. so i cannot change the select option into text field. but i be able to access the css and javascript..
Below is the select option.
<select name="siteID" id="siteID" class="abcd" style="width:100%" />
<option value='0' selected='true'> Not associated to any Circuit ID </option>
<option value='2101' > 1007345136 </option>
<option value='2102' > 1007921321 </option>
<option value='2103' > 1007987235 </option>
<option value='2407' > 132 </option>
<option value='2408' > 141 </option>
<option value='2409' > 142 </option>
<option value='2410' > 145 </option>
<option value='2701' > 225 </option>
<option value='2702' > 248 </option>
<option value='2703' > 251 </option>
<option value='2704' > 254 </option>
<option value='2705' > 264 </option>
<option value='1804' > 27 </option>
<option value='2706' > 274 </option>
<option value='2707' > 310 </option>
<option value='2708' > 311 </option>
<option value='3001' > 333 </option>
<option value='2401' > 38 </option>
<option value='2402' > 64 </option>
<option value='2403' > 68 </option>
<option value='2404' > 69 </option>
<option value='2405' > 76 </option>
<option value='2406' > 81 </option>
<option value='2411' > abc123post </option>
<option value='3301' > circuit id 50 </option>
<option value='2105' > fadhil </option>
<option value='2104' > faisal </option>
<option value='3002' > IPEN - SITE TEST </option>
<option value='3601' > Manual Circuit ID </option>
<option value='3302' > new circuit id fadhil </option>
<option value='1809' > try iframe </option>
</select>
is there any javascript/jquery and css that can transform it as serchable.
Solution 1:[1]
Ok, I understand that you cannot change the select to anything else but those who are searching for similar thing and can change the select to datalist, it is very simple:
<input list="weekday">
<datalist id="weekday">
<option value="Sunday">
<option value="Monday">
<option value="Tuesday">
<option value="Wednesday">
<option value="Thursday">
<option value="Friday">
<option value="Saturday">
</datalist>
Solution 2:[2]
You can use javascript to include other scripts like explained here:
How do I include a JavaScript file in another JavaScript file?
function includeJs(jsFilePath) {
var js = document.createElement("script");
js.type = "text/javascript";
js.src = jsFilePath;
document.body.appendChild(js);
}
Then you could solve your problem using Selectize:
includeJs("https://code.jquery.com/jquery-2.1.4.min.js");
includeJs("https://cdn.rawgit.com/brianreavis/selectize.js/master/dist/js/standalone/selectize.js");
$(function() {
$('select').selectize();
});
You will also need to include in your css the rules from selectize.css (or you could create a link element using a similar includeCss function in Javascript)
https://jsfiddle.net/qpyhjydp/
Note that in the fiddle I fixed the autoclosing <select /> (not allowed)
Solution 3:[3]
Display: none will not work foe IE11
I had same issue for search in select option.
What I did is disabled the un matched options and the hide them.
After this I have sorted the options to show only enabled options on top.
The code I have written is pasted below - please try to understand the logic I hope it will work
to disable the options use:
$("#addselect option")attr('disabled', 'disabled').hide
and to again enable it use:
$("#addselect option").removeAttr('disabled').show();
to sort by disabled options:
$("#addselect option").each(function (i, val) {
if ($(this)[i].disabled) {
moveDown("selectId");
}
else {
moveUp("selectId");
}
}
function moveUp(selectId) {
var selectList = document.getElementById(selectId);
var selectOptions = selectList.getElementsByTagName('option');
for (var i = 1; i < selectOptions.length; i++) {
var opt = selectOptions[i];
if (!opt.disabled) {
selectList.removeChild(opt);
selectList.insertBefore(opt, selectOptions[i - 1]);
}
}
}
function moveDown(selectId) {
var selectList = document.getElementById(selectId);
var selectOptions = selectList.getElementsByTagName('option');
for (var i = selectOptions.length - 2; i >= 0; i--) {
var opt = selectOptions[i];
if (opt.disabled) {
var nextOpt = selectOptions[i + 1];
opt = selectList.removeChild(opt);
nextOpt = selectList.replaceChild(opt, nextOpt);
selectList.insertBefore(nextOpt, opt);
}
}
}
Solution 4:[4]
Full option searchable select box
That also supports Control buttons keyboards such as ArrowDown ArrowUp and Enter keys
Solution 5:[5]
Check out Chosen.
I tried both select2 and selectize and neither worked properly for me. I'm using jQuery 3.6. Chosen worked first try.
Solution 6:[6]
For those who are using bootstrap in their project, a searchable select can be made using dropdown buttons. Add an input field inside the dropdown menu and use javascript to filter through the options.
A detailed explanation with example can be found on the following blog:
https://medium.com/@akshat_yadav/searchable-select-in-bootstrap-16353b93c96d
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hari Das |
| Solution 2 | Community |
| Solution 3 | Sir Celsius |
| Solution 4 | Mahdi Bashirpour |
| Solution 5 | Timothy C. Quinn |
| Solution 6 | Akshat Yadav |
