views:

509

answers:

3

Simple question really. This works in all but IE6:

<html>
<body>
</body>
<select id="test" onchange="blur()">
<option name="one">One</option>
<option name="two">Two</option>
<option name="three">Three</option>
</select>
</html>

Is there anyway to get this working in IE6? JavaScript and JQuery hacks allowed.

Any ideas, remarks or tips? Thanks in advance

+2  A: 

Under jQuery:

<script type="text/javascript">
$(function() {
    $('#test').change(function() {
        $(this).blur();
    });
});
</script>

Or you may try replacing:

With:

<select id="test" onchange="javascript:blur(this);">

UPDATE: Actually it seems that the IE6 JavaScript engine has problems with blur()

A (non-ideal) work around is:

<script type="text/javascript">
$(function() {
    $('#test').change(function() {
        $('body').focus();
    });
});
</script>

Which will lose focus on the select input field by focussing the HTML document.

Sbm007
Two mistakes in the non-jQuery version: 1) do not use a `javascript:` prefix in an event handler attribute. The value of the attribute should be pure JavaScript. 2) You should call the blur() method of the select object, not the window (which is effectively what you're doing), and I'd be surprised if what you've got there works in any browser.
Tim Down
+1 Thanks, I never knew that.
Sbm007
Actually I think I may be wrong about the second point: I think selects do not have a blur() method. Still, attempting to make the whole window lose focus seems odd.
Tim Down
hi this is closest to what we ended up using, which was body.blur() or document.body.blur().Thanks!
AlexDuggleby
A: 

What are you trying to do? If you're trying to make your drop-down lose focus after the user selects something from it, why do you want to do this?

Incidentally, if the user opens the drop-down but then selects the same option that was previously selected, you won't get a change event.

Tim Down
A: 
<select id="test" onchange="blur()">

You want to blur the select, not the window (which acts as the global object, so this is the same as window.blur()).

<select id="test" onchange="this.blur();">

Works in all but IE6 due to a bug. You could get around it by focusing something else:

<a href="#" id="x"></a>
<select id="test" onchange="var x= document.getElementById('x'); x.focus(); x.blur();">

BUT! Either way, don't do this. You'll break keyboard accessibility for the select (since pressing up/down immediately sends a change event). This is the same problem that faces drop-downs abused as navigation.

bobince