Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<option> with display:none; does not work on IE

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>?

like image 477
user1246800 Avatar asked Oct 24 '13 12:10

user1246800


People also ask

How do I set up display none?

To hide an element, set the style display property to “none”. document. getElementById("element"). style.

How do I change display none to block?

Answer: Use the jQuery css() Method You can use the jQuery css() method to change the CSS display property value to none or block or any other value. The css() method apply style rules directly to the elements i.e. inline.


2 Answers

this seems to work for me:

http://jsfiddle.net/PP4AP/1/

$('select option').each(function(){

    if(this.style.display == 'none')
    {
        $(this).remove();
    }

});
like image 95
John Boker Avatar answered Nov 03 '22 01:11

John Boker


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.

var $s = $('select');
$s.data("originalHTML", $s.html());

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/

like image 27
Luke Avatar answered Nov 03 '22 01:11

Luke