Problemas con los select en IE

Internet Explorer tiene un bug que cabrea bastante cuando le pasa a uno, éste afecta a los select. Cuando tenemos una lista de opciones de distinto ancho, es más que probable que si navegamos con IE se nos corten las opciones más largas, no adaptándose la etiqueta a éstas.

Pues bien, nuestro amigo Alberto nos envía in tip en el que se desarrolla un JS para que esto no suceda. Transcribo desde css-tricks.com ¡Espero que os solucione más de un quebradero de cabeza!

Select Cuts Off Options In IE (Fix)

I think the problem is fairly obvious here. If you set a static width on the <select> element, any <option> elements that are wider get cut of in IE 7 and below. There is no good CSS solution for this that I can come up with or find. It has been tackled with JavaScript a number of ways.

What I Did

I decided the best way to handle it is to keep the static width on the select element by default, but when moused over in IE, change the width to “auto” to accommodate. And then of course put it back to static width when moused out. I was already using jQuery so:

$(function() {

$(”.ProductAttributesSelect”)

.mouseover(function(){
$(this)
.data(”origWidth”, $(this).css(”width”))
.css(”width”, “auto”);
})

.mouseout(function(){
$(this).css(”width”, $(this).data(”origWidth”));
});

});

In plain English:

  1. When mouse pointer goes over select element, keep track of the original width, then set it to “auto”.
  2. When mouse pointer leaves, set width back to original width.

Of course, only IE 7 and lower need this, so I apply the script via IE conditional comments. Thanks to Jason Huck for reminding me to use the data function (I was setting the rel attribute to save the data before, old school style). And all the folks on Twitter for the ideas!

Leave a Reply