Skip to content

Instantly share code, notes, and snippets.

@andidev
Created September 23, 2011 15:43
Show Gist options
  • Save andidev/1237701 to your computer and use it in GitHub Desktop.
Save andidev/1237701 to your computer and use it in GitHub Desktop.
jquery browser fix for supporting disabled options in selects for IE6 and IE7
// TODO: Remove fix for selects-tags with multiple="multiple" attribute as ölong as it is not supported
// TODO: Support selects-tags with multiple="multiple" attribute
// TODO: Support disabling up and down key browsing on disabled options
// TODO: Check if selecting an an disabled option with up and down key + enter key submits form with disabled value
$(document).ready(function() {
if ($.browser.msie && parseFloat($.browser.version) < 8) {
// Add disabled color to disabled options
$("select").find("option[disabled]").css("color", "graytext");
// Select first none diabled option onload
$("select").each(function() {
var $select = $(this);
var $optionsEnabled = $select.find("option").not("[disabled]");
if ( $optionsEnabled == [] ) {
// Disable select
$select.attr("disabled", "disabled");
} else {
// Select first enabled option
$( $optionsEnabled[0] ).attr("selected","selected");
}
});
// Save selected option on load
$("select").each(function() {
var $select = $(this);
$(this).data( "lastSelectValue", $(this).val() );
});
// Hande deselect options on events
$("select").find("[disabled]").addClass("disabled");
$("select").change(function(){
selectValue(this);
});
$("select").blur(function(){
selectValue(this);
});
// Dont allow selecting disabled option
function selectValue(selectTag) {
var $select = $(selectTag);
var disabled = $select.find("[value="+$select.val()+"]").hasClass("disabled");
if (disabled) {
// Select last selected value
$select.find("[value="+$select.data("lastSelectValue")+"]").attr("selected","selected");
} else {
// Save new last selected value
$select.data( "lastSelectValue", $select.val() );
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment