views:

183

answers:

3

I am trying to do a hover effect as follows:

<div>Some Text</div>

should be substituted by a <select> upon mouseover. as soon as the mouse leaves that select it should display either the value previously displayed or the new selected value.

Unfortunately the mouseleave is called when you open the dropdown to select some options and I cannot get it to work. Has anyone had this problem before and knows the solution?

<div id="text">Some Text</div>
<select id="selector" style="display: none">
<option value="1" selected="selected">Some Text</option>
<option value="2">Some More</option>
<option value="3">Some Other</option>
</select>

the jQuery:

$("#text").mouseover(function()
{
    $(this).hide();
    $("#selector").show();
});

$("#selector").mouseleave(function()
{
    $(this).hide();
    $("#text").show();
});

Can anyone let me know how I can make sure that the mouseleave isn't called when somebody is opening the select and hovers over the options?

BTW: It does work when the mousebutton is being pressed during the selection

+1  A: 

I think that you should use the change and blur events to do this:

var hidesel=function()
{
    $(this).hide();
    $("#text").show();
}
$("#selector").change(hidesel).blur(hidesel);

in this way the select will be hidden when the user clicks on an option or clicks outside the select. I think it's also more practice.

mck89
the problem with this is, that the select does not disappear on mouseout/mouseleave. to in order to make the select lose the focus you need to actually click somewhere else... :(
@Frank: that is mck89's point (I believe): it is detrimental to the user experience to use `mouseleave` over `blur` as the "hide trigger". Forcing the user to have to perform intricate mouse gestures to accomplish their goal is detrimental. See http://www.joelonsoftware.com/uibook/chapters/fog0000000063.html (**Users can't control the mouse very well**). A click is much less prone to "accidental" gestures than a mousemove. Plus, if you go with `blur` you'd then be able to customize the UI to work with the keyboard for users so inclined.
Crescent Fresh
sorry, i missed the sentence below the code example... still, not what i'm looking for unfortunately
@crescentfresh: with only the change event the select won't hide if the user selects the options that's already selected, so i think he should use the blur event too. Anyway another solution is apply the change event to the select and then the click event on the selected option. In this way it should work without the blur event.
mck89
A: 

Have you tried using the hover() event?

$("#text").hover(
function() {
    $(this.hide();
    $("#selector").show();
},
function() {
    $(this).hide();
    $("#text").show();
});
Jagd
This suffers the same problem.
Crescent Fresh
hover doesn't work here. since it would be called infinitely.
A: 

Wrap it in a wrapper <div> and set the event handlers on that

Working Demo

jQuery

$("#wrapper").mouseover(function()
{
    $("#text").hide();
    $("#selector").show();
});

$("#wrapper").mouseleave(function()
{
    $("#selector").hide();
    $("#text").show();
});

$("#selector").change(function() {
    var value = $(this).val();
    $("#text").text($("option[value='"+value+"']", this).text());

});

HTML

<div id="wrapper">

    <div id="text">Some Text</div>
    <select id="selector" style="display: none">

        <option value="1" selected="selected">Some Text</option>
        <option value="2">Some More</option>
        <option value="3">Some Other</option>

    </select>

</div>
Russ Cam
Firefox get this right. However IE hides the select input box while leaving the options showing, Chrome suffers the exact problem as outlined in the question.
Crescent Fresh
argh - I should have tested in IE too! - one sec...
Russ Cam
very nice. but how to solve the IE issue? :( sigh...
@Russ: switch to `$('#wrapper').hover()` and all should be good.
Crescent Fresh
@cresecentfish - just tried that in IE7 and it didn't seem to make any difference :(
Russ Cam
would: $("#selector").mouseleave(function(event) { $(this).stopPropagation(); });help for the IE issue? i cannot test it... i'm on mac...
@Frank - no, not on IE7 anyway
Russ Cam
damn, this is a tricky one :(