views:

147

answers:

1

Using jQuery, I bind keydown globally like this:

$(document).bind('keydown', function(e) { alert('keydown fired'); });

When I press a key having focused a text input field, the alert shows just fine. But when I focus a checkbox and then press a key, it does not fire. Not even with the keypress event. Why do these events not bubble up to the document? They do in Internet Explorer but not in Firefox. How to I fix this? I don't want to bind the keydown explicitly to th checkbox, I want to be able to use delegation. Maybe I can't?

Thx,

/Tommy

A: 

Try binding change.

$(document).bind('keydown', function(e) { alert('keydown fired'); })
.bind('change', function(e){ alert('keydown fired'); };

Although it may be better to target check boxes specifically.

$(document).bind('keydown', function(e) { alert('keydown fired'); });
$('input:checkbox').bind('change', function(e){ alert('keydown fired'); };
Jojo
Sorry, but 'change' doesn't bubble to document either (in IE). However, I have explicitly bound the change event to my checkboxes because I really need to know when their states have changed. Then I could check for key events in the change handler and process from there... I think.
Tommy