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 hide an element, set the style display property to “none”. document. getElementById("element"). style.
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.
this seems to work for me:
http://jsfiddle.net/PP4AP/1/
$('select option').each(function(){
if(this.style.display == 'none')
{
$(this).remove();
}
});
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With