This site runs best with JavaScript enabled.

jQuery UI Dialog: OK click on Enter key

jQuery UI Dialog: OK click on Enter key jQuery UI Dialog: OK click on Enter key

I use the jQuery dialog a lot on the inzolo budget screen. It often contains small forms. After entering the form data it is natural to hit enter and expect it to submit the form. The OK button is set to post the form data via ajax. Thanks to this stackoverflow post I found a simple way to override the enter key so that when the dialog box is open, it simulates pressing the OK button. I simply added this jQuery snippet to control all my dialogs that have a form & OK button.

$('#dialog').live('keyup', function (e) {
  if (e.keyCode == 13) {
    $(':button:contains("OK")').click()
  }
})
Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.