Currently I am using jQuery to hide/show select options using following code.
$("#custcol7 option[value=" + sizeValue + "]").hide();
This works fine in Firefox, but doesnt do any good in other browsers. How to I hide options from select in Chrome, Opera and IE?
You can also replace the html within the select.
Html:
Jquery:
Not exactly what you want, but perhaps it helps. For smaller dropdowns, it is definitely easier.
using the solution provided by AuthorProxy, it works fine for IE but when I attempt to do a .val() on the dropdown in firefox I get some funky values that don't make any sense. I have modified their option to include browser specific functionality, hide/show works for firefox.
I just came across this and instead of cloning the entire select over and over I just replaced the options that need to be hidden with
span
elements and hiding the spans ( though the browser didnt visually show them anyway, I think ) - you may need to change your code ( if complex ) to iterate through the spans for complex logic.The spans store a reference to the
option
and replace themselves with it when they need to be shown.This code can obviously be refactored and prettified.
http://fiddle.jshell.net/FAkEK/12/show/
EDIT #2 ( USE THIS INSTEAD ): It occurred to me that instead of doing all this clone/reference/replace crap, just wrap the option with a span, hide the span, and on show just replace the span with the option again..
http://fiddle.jshell.net/FAkEK/25/show/
I think meder has provided valid answer and here it is slightly changed to reflect what works for me:
Tested with (long live BrowserStack):
jsfiddle
meder's solution is what I went with for this, but with a small tweak to prevent it from wrapping an option in a span that was already in a span:
To Remove options use:
To show options use: