I want to capture the Tab keypress event using jQuery, cancel the default action and call my own JavaScript function. How can I do this?
Since you mentioned in a comment that your element is dynamically inserted, you have to use delegated on()
as in your example, but you should bind it to the keydown event, because as Marc comments, in Internet Explorer the keypress event doesn't capture non-character keys:
$("#parentOfTextbox").on('keydown', '#textbox', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
e.preventDefault();
// call custom function here
}
});
See a working example in this JSBin demo.