views:

67

answers:

1

I'm listening for keypress events on an input field with delegation. For some reason, Firefox doesn't trigger the delegated event for cursor UP when at the start of the field, or cursor DOWN when at the end. LEFT and RIGHT work as expected all the time.

Directly binding an event listener to the field works fine, so it has to be something to do with delegation. Does anyone know if this is a know issue, I couldn't find anything on Google/forums etc..?

$("div").delegate(":input", "keypress", function(e){
  // doesn't get triggered
});

$("div :input").bind("keypress", function(e){
  // gets triggered fine
});

Here's a demo which shows the issue - http://livsey.org/jquery.delegation.html

A: 

These keys don't bubble in Firefox, at least not in that case, so .delegate() or .live() won't work. This is a known issue, better to use a different event in this case, like keydown or keyup, you can see the jQuery documentation for .keypress() for a quick blurb about this:

Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.

Updating your code to this works:

$("div")().delegate(":input", "keyup", function(e){
  log("delegated: "+e.keyCode);
});

$("div :input").bind("keyup", function(e){
  log("bound: "+e.keyCode);
});
Nick Craver
Well his event handlers are definitely being called; I think the problem is that Firefox only sets "keyCode" in "keyup" and "keydown" event loops, not "keypress".
Pointy
@Pointy - His bound event is being hit...not the delegated one depending on the bubble, no character was actually entered, so nothing to bubble.
Nick Craver
@Nick ?? I get a message from his delegate handler when I try his page.
Pointy
@Pointy - I'm not seeing that, you using firefox?
Nick Craver
Yes, Firefox on Windows - I get two messages per keypress, one saying "delegated" and one saying "bound" (the values are always zero however). It's pretty weird that you wouldn't see them, and I can't explain that.
Pointy
@Pointy - Using up arrow at beginning, down at the end, I get `bound: 38` and `bound: 40` respectively (no `deleate:`), Firefox 3.6.3 here, doing the same test/same version on your end?
Nick Craver
The weird thing is that it only doesn't work for UP when the cursor is at the start of the field, and likewise for DOWN when at the end of the field. In all other positions it works fine.I'm using keypress instead of keyup/keydown because I need to detect repeat events when the key is held down (in Safari you get these on keydown but for firefox you only get them on keypress as far as I can tell).
Sorry, should have mentioned browser version/OS - Firefox 3.6.3 on OSX
@rlivsey - Presumably because those keys have no "effect" on the field, not even changing the cursor position, so Firefox feels no need to bubble the event? I'm not sure of their reasoning there, but that's the best I can figure.
Nick Craver
That makes sense, time for some coffee and a late night to figure out a workaround! Cheers.
Looks like textarea's bubble fine under these circumstances, I've updated the demo page to this effect.
@rlivsey - The `<textarea>` would definitely handles these differently, since it actually has multiple rows, the best bet for information on this would probably be mozilla development notes of some sort, but I'm not sure where you'd start on finding anything like that...
Nick Craver