Reputation: 62384
I'm trying to simulate a keypress with the below code...
jQuery('input[name=renameCustomForm]').live('keyup', function (e) {
console.log('pressed');
});
jQuery('body').click(function (e) {
console.log(jQuery('input[name=renameCustomForm]'));
var press = jQuery.Event("keypress");
press.which = 13;
jQuery('input[name=renameCustomForm]').trigger(press);
});
I got this code from other posts on SO, but it doesn't work. Anyone know why?
Update
Fixed it... it appears triggering "keypress" doesn't automatically trigger "keyup"
Upvotes: 2
Views: 2539
Reputation: 348992
Normally, when a user adds something to an inout field, the following events occur:
keydown
(once).keypress
(at least once, additional events uccur while the key is pressed down)keyup
(once)When a key event is simulated, it's not necessary that all events occur in this order. The event is manually dispatched, so the normal event chain isn't activated.
Hence, if you manually trigger the keypress
event, the keyup
event won't be fired.
Upvotes: 2
Reputation: 195992
You code will trigger a keypress
each time you click anywhere on the page..
For your case it might be better to use the .blur()
event of the input box..
jQuery('input[name=renameCustomForm]').live('keyup', function (e) {
console.log('pressed');
}).live('blur', function(){
var self = $(this);
console.log( self );
var press = jQuery.Event("keyup");
press.which = 13;
self.trigger( press );
});
Upvotes: 0