This site runs best with JavaScript enabled.

jQuery UI Dialog: OK click on Enter key

Simple jQuery snippet to close a dialog box on enter

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.

1$('#dialog').live('keyup', function (e) {
2 if (e.keyCode == 13) {
3 $(':button:contains("OK")').click()
4 }

Discuss on TwitterEdit post on GitHub

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.

Join the Newsletter

Dustin Davis