4

When I try to load the select options into the optStored array using the array push, the array always remains empty. In chrome, firefox, and safari the code is working correctly but not in Internet Explorer. Is there a work around for this or nothing can be done about this?

Updated 2:12am 10/11/16: Made some changes to the code, previous code is commented out but the array still remains empty! Is there a solution?

// array to store select options
var optStored = [];

// filter select 
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);
      }
    }
  }
}

// Restore select options
function restoreOpts() {
  var sel = document.getElementById('fruits');
  sel.options.length = 0;
  for (var i = 0, iLen = optStored.length; i < iLen; i++) {
    // Recent Update 2:12am 10/11/16:
    // Found aworkaround for restoring the select options 
    // This method works with versions of IE4+
    sel.options[sel.options.length] = new Option(optStored[i].text, optStored[i].value, false, false);

    // Recent Comment Update 2:12am 10/11/16: 
    // Console complains of a type mismatch error
    // sel.add(optStored[i], null); 
  }
}

// Load select options
window.onload = function() {
  var sel = document.getElementById('fruits');
  for (var i = 0, iLen = sel.options.length; i < iLen; i++) {
    // Recent Comment Update 2:12am 10/11/16: 
    // tried this method as well but no luck. 
    // it was also mentioned in the comments below
    // the array still remains empty!
    optStored[i] = sel.options[i];

    //optStored.push(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>
ur345
  • 41
  • 4
  • What versions do you mean by "older versions of IE"? Maybe those versions don't have the `add` method on a Select element? Maybe it has that method, but it takes different arguments (like a String instead of an ` – Greg Burghardt Oct 10 '16 at 17:05
  • 1
    old versions of IE5 – ur345 Oct 11 '16 at 00:23
  • 4
    Wait, are you into retrocomputing? IE5, lol. – Bergi Oct 11 '16 at 02:22
  • Man, IE5 is Windows 98-era. It's market share was already [below 1%](https://en.wikipedia.org/wiki/Internet_Explorer_5) at the end of 2006, and there is virtually _nobody_ still using it today, except perhaps nostalgists. I'm interested -- why do you need this? – John Weisz Oct 11 '16 at 06:52
  • It's for school. If it was easy I would've use jquery, angular, etc and be done with it but unfortunately these are the requirements :/ – ur345 Oct 11 '16 at 06:57
  • 1
    I wish I could have a few words with your teacher, but... eh. Bergi posted a good approach, basically you could use the `length` property to implement a `push` yourself, easily. There is also one over here: http://stackoverflow.com/q/31533963/2788872 – John Weisz Oct 11 '16 at 07:59

2 Answers2

3

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push#Browser_compatibility, IE does support the push method only as of version 5.5. Try the following instead:

window.onload = function() {
  var sel = document.getElementById('fruits');
  for (var i = 0, iLen = sel.options.length; i < iLen; i++) {
    optStored[i] = sel.options[i];
  }
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

If you press F12, does it give any errors in the console? The IE console is nowhere near as good as the Chrome/Firefox developer tools, but it should display any errors.

I'm wondering about passing undefined as the second argument to the add method, according to the docs it should null to append the new option to the end of the list

before is optional and an element of the collection, or an index of type long, representing the item item should be inserted before. If this parameter is null (or the index does not exist), the new element is appended to the end of the collection.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/add

danwellman
  • 9,068
  • 8
  • 60
  • 88
  • Wait, IE5 had a developer console? – John Weisz Oct 11 '16 at 06:54
  • 1
    The question has been changed, originally it just said 'old versions of IE' without specifying a particular version. Calling it a 'developer console' is a bit of a stretch, but it almost certainly had a way to at least view errors. There were also plugins for IE that gave some kind of development experience, tools such as [Debug Bar](http://www.debugbar.com/) [*shudders*] – danwellman Oct 11 '16 at 08:21