Reputation: 1241
When you press enter
on the keyboard, a form is sent automatically.
I wish this were not the case only if a specific field (#myText
) has focus when you press enter
.
Thanks to this, I have this code now:
$('#myText').on('focusin', function() {
console.log('focusin');
$(window).keydown(function(event){
if(event.keyCode == 13) {
console.log('enter!');
event.preventDefault();
return false;
}
});
});
But it doesn't work well, because when the field loses the focus, pressing enter
nothing happens.
Where am I wrong?
Upvotes: 2
Views: 472
Reputation: 8591
You aren't binding your keydown event to the specific input. Change this line:
$(window).keydown(function(event){
to this:
$(this).off('keydown').keydown(function(event){
Upvotes: 3