views:

915

answers:

6

I want to highlight a select element with a background color to indicate, it is mandatory. When the user opens the menu by clicking on it, I want to remove the background color, so it looks nicer and is more readable. This works just fine in Firefox, Chrome and even IE6, but on IE7 & 8 the pulldown doesn't open on the first click (or is opened and closed very fast), only on the second.

<select 
 style="background-color: #BDE5F8"
 onfocus="this.style.backgroundColor='#fff'"
 onblur="this.style.backgroundColor='#BDE5F8'">
 <option>choose...</option>
 <option>1</option>
 <option>2</option>
 <option>3</option>
</select>

How can I fix this?

A: 

Try onfocus="this.style.backgroundColor='#fff';return true;"

The return true; means that the original event handling code should continue. Also try whether you can achieve the same result with CSS.

Aaron Digulla
I tried this, does not help. Did you actually try it?
Tim Büthe
A: 

After testing a bit more, it seems to me that the best way would be to change the color onmouseover, and change it back on onblur, like

<select 
    onmouseover="this.style.backgroundColor='#fff';"
    onblur="this.style.backgroundColor='#bde5f7'">
    <option>choose...</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

Since navigating the dropdown with a keyboard doesn't show the options, it doesn't seem that important that the event has to be an onfocus event.

It appears the onmouseout event gets triggers as soon as you are no longer over the actual <select>, but maybe this can be solved with some jQuery eventhandling?

Otherwise, I have no idea. :)

Marco
A: 

Seems like a very odd bug indeed. It apparently lies on setting any different setting during the focus, for example, a class to the select.

One idea would be to only set a 'required' background color on the Choose... option as a workaround (that doesn't work on other browsers, do a browser check for this one).

<select>
        <option style="background: red none">choose...</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
</select>
F.Aquino
That would be a nice workaround, I'll try this one too.
Tim Büthe
+5  A: 

After a little bit of testing, it appears to me that IE doesn't open the dropdown if the style is modified in any way.

Yeah, good bug catch there. Anything that changes the select box (including any style change, even one triggered by changing a parent className) makes IE recreate the OS widget for it, which has the side-effect of closing it. So the dropdown is opened, but immediately closed before rendering. If you put a timeout on the background change function you can see it happen afterwards.

What you would need would be an event first just before focusing, so you can change the style, causing the dropdown to close, before it opens. Luckily, there is one! But it's IE-only. For other browsers (including IE8), best stick to the simple CSS :focus selector:

<style>
    select { background-color: #BDE5F8; }
    select:focus, select.focus { background-color: white; }
</style>
<select>
        <option>choose...</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
</select>

<!--[if lt IE 8]><script>
    // Fix lack of :focus selector for select elements in IE7-
    //
    var selects= document.getElementsByTagName('select');
    for (var i= selects.length; i-- >0;) {
        var select= selects[i];
        select.onfocusin= function() {
            this.className= 'focus';
        };
        select.onfocusout= function() {
            this.className= '';
        };
    }

    // Or, the same expressed in jQuery, since you mentioned using it
    //
    $('select').bind('focusin', function() {
        $(this).addClass('focus');
    }).bind('focusout', function() {
        $(this).removeClass('focus');
    });
</script><![endif]-->
bobince
In other words, it isn't possible the way I want it, thanks for the description and the IE :focus workaround. The problem is, I can't use this workaround for IE, because my JavaScript is a bit more complicated then the provided code. E.g. I only highlight the select if a specific option is selected and I do the same thing onchange, that I do onblur. However, since I know it isn't possible, I'll just change my code and exclude IE-Users from that function.
Tim Büthe
A: 

I've spent quite a while trying to resolve this issue and found a simple work-around for IE8 - just use onmousedown rather than onfocus. While this may not be absolutely ideal in all situations it does work well for style changes.

hippyneil
A: 

Here is my example of this bug for ie8:

http://jsfiddle.net/axQTJ/1/

thetoolman
And yes, it seem that if an added class changes the dropdown presentation, it reloads and therefore doesnt drop down. Interestingly, the border property is external, and can be changed without this bug occuring.
thetoolman