0

I am developing a webpage with a form with several <input type="submit"> and one <input type="password">. It is a multistep form managed by ajax callbacks.

The reason I am using more than one Submit per form is because Drupal Ajax callbacks are called by them. My problem and question is the following:

Is there any way to bind "enter" to a certain submit?

I have tried this piece of code (using jQuery):

$('#myInput').on('keyup', function(event) {
    // I have tried event.preventDefault(); here
    if (event.keyCode == 13) { // 13 stands for Enter key
        // And here
        $('#desiredSubmitInput').click();
    }
});

But I think there is some kind of async because "Enter" behavior is happening before the click and therefore submits a random submit (I don't even know if it is random or taking first/last submit, not the desired one).

I have even tried to preventDefault the keyUp event but it neither works.

Thank you!

  • instead of `keyup` use `keypress` event – Faheem azaz Bhanej Dec 18 '21 at 09:39
  • 1
    and also prevent the default behavoir with event.stopPropagation() – Rafael Herscovici Dec 18 '21 at 09:42
  • @FahimazazBhanej It really works as desired If I preventDefault inside the if. Thanks for your comment! Would you like to answer it so I mark it valid? Thanks! – RobGherDev Dec 18 '21 at 09:42
  • @Dementic True! I just read a question related to the difference of preventDefault and Stop here https://stackoverflow.com/questions/5963669/whats-the-difference-between-event-stoppropagation-and-event-preventdefault and it is true! Thanks! – RobGherDev Dec 18 '21 at 09:44

0 Answers0