Today I had the same problem. Without removing unwanted parameters, you will not get the expected result. But the problem is that you need to remember these parameters if you want to display them later.
My solution is very simple and works in all major browsers:
function filterList(oList, rxSearch) { // Create backup of the old option list (only once) if(typeof(oList.tagOptions) === "undefined") { // Save current options oList.tagOptions = [ ]; // Generate a dynamic property for this DOM object for(var i = 0; i < oList.options.length; ++i) oList.tagOptions.push(oList.options[i]); } // Clear the current option list while(oList.options.length) oList.options.remove(0); // Add only those options which match the regular expression for(var i = 0; i < oList.tagOptions.length; ++i) { if(rxSearch.test(oList.tagOptions[i].text)) oList.options.add(oList.tagOptions[i]); } }
The trick is that option elements will be copied to the dynamically created tagOptions property on first launch. Since links (in tags) will be stored in these DOM methods, they will not be released. In addition, you do not need global variables for this. Later, visible parameters (oList.options) will be cleared and only those options that match the search query will be added.
Using the following HTML code:
<select id="myList" size="10"> <option>apple</option> <option>advocado</option> <option>banana</option> <option>coconut</option> </select>
You would call it this way:
filterList(document.getElementById("myList"), /^a/i); // Display only those elements, which start with an a alert("Display all with a"); filterList(document.getElementById("myList"), /^b/i); // Display only those elements, which start with an b alert("Display all with b"); filterList(document.getElementById("myList"), /^/i); // Display only those elements, which start with an c alert("Display all");
I tested this with Firefox, Internet Explorer 11, Chrome, and Opera. It works great for my purposes.
function filterList(oList, rxSearch) { // Create backup of the old option list (only once) if(typeof(oList.tagOptions) === "undefined") { // Save current options oList.tagOptions = [ ]; // Generate a dynamic property for this DOM object for(var i = 0; i < oList.options.length; ++i) oList.tagOptions.push(oList.options[i]); } // Clear the current option list while(oList.options.length) oList.options.remove(0); // Add only those options which match the regular expression for(var i = 0; i < oList.tagOptions.length; ++i) { if(rxSearch.test(oList.tagOptions[i].text)) oList.options.add(oList.tagOptions[i]); } } filterList(document.getElementById("myList"), /^a/i); // Display only those elements, which start with an a alert("Display all with a"); filterList(document.getElementById("myList"), /^b/i); // Display only those elements, which start with an b alert("Display all with b"); filterList(document.getElementById("myList"), /^/i); // Display only those elements, which start with an c alert("Display all");
<select id="myList" size="10"> <option>apple</option> <option>advocado</option> <option>banana</option> <option>coconut</option> </select>
source share