When I try to load selection parameters into the optStored array using the push array, the array always remains empty. In chrome, firefox and safari, the code works correctly, but not in Internet Explorer. Is there any work for this or nothing can be done about this?
Updated 2:12 AM 10/11/16: Made some changes to the code, the previous code is commented out, but the array is still empty! Is there a solution?
var optStored = [];
function filterFruits(el) {
var value = el.value.toLowerCase();
var form = el.form;
var opt, sel = form.fruits;
if (value == "") {
restoreOpts();
} else {
for (var i = sel.options.length - 1; i >= 0; i--) {
opt = sel.options[i];
if (opt.text.toLowerCase().indexOf(value) == -1) {
sel.removeChild(opt);
}
}
}
}
function restoreOpts() {
var sel = document.getElementById('fruits');
sel.options.length = 0;
for (var i = 0, iLen = optStored.length; i < iLen; i++) {
sel.options[sel.options.length] = new Option(optStored[i].text, optStored[i].value, false, false);
}
}
window.onload = function() {
var sel = document.getElementById('fruits');
for (var i = 0, iLen = sel.options.length; i < iLen; i++) {
optStored[i] = sel.options[i];
}
};
<form>
<input type="text" onkeyup="filterFruits(this);" placeholder="Filter">
<select id="fruits">
<option value="1">Apple</option>
<option value="2">Orange</option>
<option value="3">Cherry</option>
<option value="4">Banana</option>
</select>
</form>
Run codeHide result
source
share