tags:

views:

26

answers:

2

Hi,

    $(function() { 
   $('#IBE1_IBE_NurFlug1_ddl_Abflughafen > option[value*="TR"]').attr("style", "display: none;");

does not hide options in a select. Also .hide does not work. What's wrong here? In Firefox it's ok.

Thx a lot

+4  A: 

Firefox for some reason is the only browser I've used that let's you modify <option> elements via CSS.

In truth, you cannot hide <option> elements - they must be removed from the <select>'s options[] array.

Edit:

Example:

(function () {
    var select = $('#IBE1_IBE_NurFlug1_ddl_Abflughafen');
    var diff = 0;
    console.dir(select[0].options);
    select.find('option').each(function(x) {
        if ($(this).is('[value*="TR"]')) {
            select[0].options[x+diff] = null;
            diff -= 1;
        }
    });
}());​

You can see the example live here.

The idea is to pull ALL <option> children from the <select>. This is necessary for proper indexing (so we remove the right thing).

We then iterate through the <option>s and run your selector against them. If it matches, we remove that option based on the index passed by each.

The diff is counting the removals so we do not remove the wrong <option> as the options[] array decreases in size from previous deletions.

Matt
could you please provide an example?
@ceyago - certainly. See my update.
Matt
sorry. didn't notice your edit. I will try it, thx. But I want to hide the option instead of .remove. Cause I need to show it back later.
@ceyago - in that case, you're probably going to have to manage a separate array that keeps track of the hidden options so they can be added back later
Matt
In Safari it's ok with remove. Unfortunately Firefox is making trouble.http://jsfiddle.net/Axaag/10/Could you please help me again with the separate array issue?
I think I will try disabling the options instead of hiding.http://jsfiddle.net/Axaag/12/thx a lot Matt, esp. for jsFiddle. Didn't know it.
A: 

Assuming you are happy to remove the option and do not require it later then this would be simpler...

$('#IBE1_IBE_NurFlug1_ddl_Abflughafen option[value*="TR"]').remove();
Jimmy