Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change event not fired on return key

Tags:

jquery

I have a textbox with a jquery change event tied to it.

$('#txtAccountNo').change(function(){
        $('label[for="txtAccountNo"]').append('<span class="support">loading...</span>')
    });

The event works when the textbox loses focus, ie the user clicks out of it with the mouse or uses the tab key. But when the return key is pressed the event is not fired. Can anybody help me resolve this please?

like image 423
Stuart Avatar asked Feb 08 '13 10:02

Stuart


1 Answers

This is standard behaviour for the change event. If you want to capture each Enter press you need to use the keypress event:

$('#txtAccountNo').on('change keypress', function(e) {
  if (e.type == 'change' || (e.type == 'keypress' && e.which == 13)) {
    $('label[for="txtAccountNo"]').append('<span class="support">loading...</span>')
  }
});
like image 115
Rory McCrossan Avatar answered Sep 19 '22 21:09

Rory McCrossan