Rajasekhar
Rajasekhar

Reputation: 2455

"Enter" Keypress to Submit the Form is not working in IE 9/10

$(function(){
    $('.inviteClass').keypress(function() {
        if(event.keyCode=='13') {
            doPost();
        }
});

Here I have one small requirement. Pressing keyboard Enter to submit the form and it is working fine in FireFox and Chrome, as well as IE 7 and 8, but it is not working in IE9 and IE 10.

Please help me.

Upvotes: 0

Views: 9505

Answers (4)

Samuel Liew
Samuel Liew

Reputation: 79022

Points to note:

  1. You are missing a closing bracket.
  2. Also, change the selector to window
  3. Use .on() function
  4. Use the .which property of event. See jQuery documentation
  5. The keycode is an integer - remove the quotes
  6. Add a return false; to stop the event from bubbling to the form (and possibly submitting the form twice). See Submitting a form on 'Enter' with jQuery?

Final code:

$(function() {
    $(window).on('keydown', function(event) {
        if(event.which == 13) {
            doPost();
            return false;
        }
    });
});

Upvotes: 8

alijsh
alijsh

Reputation: 860

you must use jQuery's event.which, also change '13' to 13 (a closing bracket was also missing):

$(function(){
    $('.inviteClass').keypress(function(event) {
        if(event.which == 13) {
            doPost();
        }
    });
});

Upvotes: 0

Madhu Magar
Madhu Magar

Reputation: 445

Please Try to use keydown event and also pass the event object in the function like this

$(function(){$('.inviteClass').keydown(function(event){if(event.keyCode=='13'){doPost();}});

or

$(function(){$('.inviteClass').keypress(function(event){if(event.keyCode=='13'){doPost();}});

Hope this will help you

Thanks

Upvotes: -1

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

try

$('.inviteClass').keypress(function (e) {
  c = e.which ? e.which : e.keyCode;
  if (c == 13) {
    doPost();
    e.preventDefault();
    return false;    //<---- Add this line
  }
});

Upvotes: 1

Related Questions