I have a keydown
event binding that is attached to elements which have the class numbersOnly
which looks like this:
$('.numbersOnly').keydown(function(e) {
// Determine which key was pressed and ignore if not a number.
});
This works on all elements which were added on page load. However, if I add the class to an element dynamically using .addClass()
, the event does not fire on that element. I've tried changing .keydown(
to .on('keydown'
, but it makes no difference (I know it shouldn't - they should be the same).
I'm using jQuery 1.9.1 but I've also tested this in 2.x and it doesn't work there either. Here's a fiddle with a working example: