1

How do I trigger the same JQuery function when either:

  • the form submit button is clicked
  • enter is pressed
Omar
  • 32,302
  • 9
  • 69
  • 112
Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
  • 1
    use this as stated in the answer below. `.on('submit', function` – Omar Jun 21 '13 at 08:24
  • Yes, this is actually the answer Omar. If you add this as an answer I'll mark it as correct. It was a bit of a silly question to ask. Of course I should be just handling a 'submit'. I've made this mistake before, that a 'click' on a submit button is a separate event, but obviously results in a submit, which is the same thing that happens on pressing 'enter'. Because I'm currently only handling a click on the submit button, for some reason my mind went over to also needing to handle a click on 'enter', instead of simply handling the submit. – Chris Halcrow Jun 24 '13 at 02:30
  • Ah - sorry, the question was confusing. Have edited it now. Fair enough to downvote it. Really it's a basic question I'm asking, just got in a muddle, however hopefully it'll help someone else who's either new, or like me just not thinking straight! – Chris Halcrow Jun 24 '13 at 02:33

1 Answers1

1
You can use the keycode for this.

var keycheck = (e.keyCode ? e.keyCode : e.which);
 if(keycheck == 13) { // keycode for Enter key
   //Do something
 }
karthi
  • 889
  • 12
  • 24