views:

29

answers:

3

This is a weird bug, indeed. In Chrome (6.0.472.62, latest) and IE8 (at least), this behaves correctly, but in FF (3.6.9, latest) both the click event and enter event register, making it hard to discern between the behavior.

Check out this code: http://jsfiddle.net/QmkwY/1/, click on the search box in the "results" and just hit enter. The results underneath should register click event: 1 enter event: 13, which is clearly incorrect.

I have different things happening for click events and enter events on my page, so when an enter event registers as a click event, you can imagine the frustration!

Anyone have a clever solution?

A: 

You are binding to event to '#button' it should be '#search'

mikerobi
@mike i know, what i'm saying is i have two events bound to two separate inputs. the problem is both fire with two different event codes in FF
Jason
@Jason, you should fix your example to match your question. You are binding keyup events and not enter events. I get the exact same behavior in chrome and firefox.
mikerobi
@mike the example is obviously contrived. in my actual code i filter out based on `key==13` which is the enter key. for this quickly whipped up example, i'm just trapping any keyup. there's no way to natively trap enter presses in jquery or javascript, as far as i know
Jason
+1  A: 

In clickEvent, you can check e.pageX and e.pageY to be sure they have values to see if it was actually clicked.

if (e.pageX == 0 && e.pageY == 0)
{
    return;
}

But that will also affect "clicking" the button via spacebar. If that's not ok, you'll need to bind spacebar to the button separately.

$('#button').keyup(function (e)
{
    if (e.which == 32)
    {
        // do something
    }
}
gilly3
ha, interesting. i will try this out. thanks
Jason
it's crazy, but it works... i'd like to think there's a more, erm, "correct" way to solve this, but for the time being, this will do. thanks!
Jason
I think the "correct" way would be for FF to not fire the event. But, checking for the mouse position should be reliable.
gilly3
A: 

Well, when your button is hidden I'm only getting the enter event, and not the click event in Chrome. However, when I show the button I get both. I also inserted a button between your button and the input, and that causes it to not fire the click event.

I believe this is intended as a shortcut to submit forms, you could do workarounds as others have posted, but I don't think this is a real 'problem.'

Robert
it is a real problem in Firefox, as stated in the question. i'm aware that the behavior is correct in Chrome/IE
Jason
The click event still fires in Chrome under certain situations, and I believe it's intended for that to happen.
Robert