1

On the web page I have a listbox with a few items in it. The line:

    $('#my-list-of-options option:selected').hide();

successfully hides the element currently selected element in mozilla. Not in chrome though. How can I get it to work in chrome?

PSL
  • 123,204
  • 21
  • 253
  • 243
Ya.
  • 1,671
  • 4
  • 27
  • 53

3 Answers3

1

display:none does not work across browser for option. You can try to make the option disabled

 $('#my-list-of-options option:selected').prop('disabled',true);

Fiddle

Or remove the option and rebind it when required. Save it in the select data for latter restore, something like this.

Fiddle

PSL
  • 123,204
  • 21
  • 253
  • 243
  • That does disable the option but it's still visible. Ideally I'd like to make it disappear altogether but without physically removing it from the list. – Ya. May 30 '13 at 00:50
  • Yes thats what disabled do. try the second solution. – PSL May 30 '13 at 00:51
  • 1
    This may not be the answer you wanted, but its the correct one. – j_mcnally May 30 '13 at 00:51
  • @Ya. also if you want to restore a particular option you can easily apply selector on the stored `data` and extract it out, or filter out the unwanted and populate them. – PSL May 30 '13 at 00:53
  • I intended to mark yours. – Ya. May 30 '13 at 01:20
  • @Ya. ok. I was wondering what went wrong and what you were expecting.. :) – PSL May 30 '13 at 01:23
1

The css display:none on option elements is not supported in chrome (or ie).

The only choice is to remove the element:

$('#toppings').change(function(){
    var selected = $('#toppings option:selected');    
    selected.remove();    
});

Working fiddle: http://jsfiddle.net/basarat/777Jm/

basarat
  • 261,912
  • 58
  • 460
  • 511
0

Here is a great refcard on jQuery selectors (though may have to sign up to download it).

$('#my-list-of-options option[selected]').hide();
Shadow Man
  • 3,234
  • 1
  • 24
  • 35