I have some style='display:none'
in the option element, it work well on Chrome and I realise it does not work on IE.
<select>
<option style="display:none;">One</option>
<option>Two</option>
<option style="display:none;">Three</option>
<option>Four</option>
</select>
Using jQuery, how to loop through the option to find display:none
and remove the elements <option>
?
to disabled the options use
John Boker's solution is the right one for this question. But it does have the downside that you won't be able to ever retrieve those options once you've removed them.
One solution is to save the full HTML of the
<select>
before you remove any<option>
tags.Now you can easily restore by reversing this:
$s.html($s.data("originalHTML"));
Full details are on this solution: https://stackoverflow.com/a/24439289/1766230
Also an example: http://jsfiddle.net/luken/9CYjy/
this seems to work for me:
http://jsfiddle.net/PP4AP/1/
I had the same issue but bit complex , and yes we need to remove the option to hide in IE/Chrome/Safari.
But in some cases we want to just HIDE group of options based on criteria /filter and
remove()
just removes completely. I have following solution to this problem. It removes the options but keeps removed option in a data attribute on sameselect
element and when desired we just re-populate in given sorting order ( value or label).Let say:
(require Jquery):
You can pass a more complex filter function to remove the desired options.
Working Demo
Today I had the same problem. Without removing the unwanted options you won't get your expected result. But the problem is that you need to memorize those options if you want to display them later.
My solution is very simple and works in all major browsers:
The trick is that the option elements will be copied into the dynamically created tagOptions property on the first run. Since there will be still references (in tagOptions) to these removed option DOM elements they will not be released. Furthermore you need no global variables for that. Later the visible options (oList.options) will be cleared and only those options added, which match the search term.
Using the following HTML code:
You would call it like this:
I've tested this with Firefox, Internet Explorer 11, Chrome and Opera. It works fine for my purposes.