I'm trying to use jQuery's keypress to trigger a button click on a modal dialog created using the jQuery dialog function. The problem is, with the following code, it works the first time around (pressing enter presses the Save button) but I get erratic behavior when I close the modal dialog and reopen it. I'm thinking some variant of $(this).("button:contains('Save')")
would work, but that doesn't work.
$('#dialog').keypress(function(e) {
if (e.which == 13) {
$("button:contains('Save')").click();
}
});
FYI the dialog is opened using a $("#dialog").dialog('open'), not an autoOpen:true. What would be the best practice for the task?
Thanks!