Hide OPTIONS in select box with jQuery
It seems to be a pretty simple task to hide or show an <OPTION element using a jQuery.
$("select option").hide(); //hide elements
$("select option").show(); //show elements
Can’t be easier? However we have a “Great” browser Internet Explorer, that does not understand it. Playing with different possibilities I found one tricky solution to wrap option element with the SPAN tag.
Also make sure we do “wrap” only for Internet Explorer. Otherwise you will have issues with the jQuery .val() method. it will give you incorrect result;
//hide elements
$("select option").each(function(index, val){
if ($(this).is('option') && (!$(this).parent().is('span')))
$(this).wrap((navigator.appName == 'Microsoft Internet Explorer') ? '<span>' : null).hide();
});
//show elements
$("select option").each(function(index, val) {
if(navigator.appName == 'Microsoft Internet Explorer') {
if (this.nodeName.toUpperCase() === 'OPTION') {
var span = $(this).parent();
var opt = this;
if($(this).parent().is('span')) {
$(opt).show();
$(span).replaceWith(opt);
}
}
} else {
$(this).show(); //all other browsers use standard .show()
}
});
Dima Svirid
Software architect. Ajax/Javascript, HTML5, Android, iPhone/iPad, JAVA, PHP, Cold Fusion, SQL, Air, Flash, Open source software, Frameworks
SEARCH ARTICLES & TUTORIALS
MOST POPULAR TUTORIALS
Generating PDF Documents with Adobe Air Javascript SDK and jQuery
I am not a Flex guy and I don't think I will ever be. While searching [...]
Search Engine Optimization - Do It Yourself!
This is Part 1 of "Search Engine Optimization – Do It Yourself!" ser[...]
jQuery Object Oriented Plugins
Many people have asked me, if jQuery is object oriented and how they c[...]
Spring-MVC Tutorial (Part 1)
Overview This document is a step-by-step guide on how to de[...]
Animated scroll to anchor method with jQuery
It's a one line but can add a really nice effect to your website. f[...]
RECENT POSTS
Is trying to be everywhere online actually helping grow your business?
Social media experts have probably told you that keeping your clients [...]
How to calibrate Samsung Galaxy battery
There is no guarantee that this method will work for you. However it d[...]
Real Estate Social Network new Toronto Startup
Wanted to write for a long time about a new startup for Real Estate ag[...]
Eclipse Keyboard Shortcuts for Developers
Eclipse Navigation Shortcuts Every Java Programmer Should Know Ctrl [...]
Duplicated Content
This is Part 4 of "Search Engine Optimization – Do It Yourself!" ser[...]




