how to close combobox when onmouseout?

How to close combobox when onmouseout?

<select onmouseout="">
 <option value="volvo">Volvo</option>
 <option value="saab">Saab</option>
 <option value="mercedes">Mercedes</option>
 <option value="audi">Audi</option>
</select>

Answers:

Answer

You can't do this (reliably). The popup that appears is not available to manipulate, ie open or close, programmatically and its behaviour is defined by the browser (or operating system).

To expand on this further, both IE and Firefox can close the popup by blurring the select element:

selectEl.blur();

Although, the mouseout event fires even when you move the mouse to the options in the popup, so it would require a bit of hackery magic. In Chrome it will blur the select element but the box will remain open.

It's generally best to leave the behaviour of UI components alone, so that users get the experience they expect through interaction with your website.

Answer

You can do a little trick like this...

<select onmouseover="size = 5" onmouseout="size = 0">
 <option value="volvo">Volvo</option>
 <option value="saab">Saab</option>
 <option value="mercedes">Mercedes</option>
 <option value="audi">Audi</option>
</select>

This will expand the menu on mouse over then collapse it when you move out.

Edit: This can cause issues with placement of other elements if you're not careful.

Answer

A couple of years late but in case someone else is looking at this...I was able to toggle the open/close state of a select element in Chrome by sending it a mouse click. In this example moving the mouse over the toggle button will toggle the state of the <select>. This is a toggle, not an explicit open or close, but worked for me.

<script type="text/javascript">

    function clickSelect(element) {
        var event = document.createEvent('MouseEvents');
        event.initMouseEvent('mousedown', true, true);
        element.dispatchEvent(event);
    };

    function toggleSelect() {
        clickSelect(document.getElementById("select"));
    }  
</script>


<form>
    <select id="select">
        <option>one</option>
        <option>two</option>
    </select>
    <input type="button" onmouseover="toggleSelect();" value="toggle select state" />
</form>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.