Skip to content

Instantly share code, notes, and snippets.

@zrod
Created November 17, 2011 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zrod/1373141 to your computer and use it in GitHub Desktop.
Save zrod/1373141 to your computer and use it in GitHub Desktop.
Fixed width for IE Select elements (< 9)
$(document).ready(function() {
if ($.browser.msie && $.browser.version.charAt(0) < 9) myScript.ieSelectFixedWidth('select.mini, select.medium');
}
// ...
var myScript = {
ieSelectFixedWidth: function(el) {
$(el).each(function(k, v) {
var obj = $(v);
if (obj.length > 0) {
var objWidth = obj.outerWidth(),
objContainerId = obj.attr('id') + '_options',
objContainer = $('<span id="' + objContainerId + '" class="ieSelectFixedWidthContainer" />').css('width', objWidth),
widthSet = false;
obj.wrap(objContainer);
obj.bind("click mousedown focusin", function () {
displaySelectOverlay();
});
obj.bind("blur change focusout", function() {
hideSelectOverlay();
});
function displaySelectOverlay() {
if (!widthSet) {
obj.css({'position': 'relative', 'width': 'auto'});
if (obj.outerWidth() < obj.parent().innerWidth()) {
obj.css('width', objWidth);
widthSet = true;
}
}
}
function hideSelectOverlay() {
obj.css('width', objWidth);
}
}
});
}
};
.ieSelectFixedWidthContainer {
position: relative;
overflow: hidden;
display: inline-block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment