Search code examples
jqueryjquery-ui-dialog

How do I make my code set focus to a specific button in a jQuery dialog?


I have code to open a jQueryUI dialog with two buttons (OK, and Cancel). When the dialog opens, I want the OK button to have focus by default. After that, when I press the ENTER key, it should perform the actions associated with the OK button. How do I make this happen?


Solution

  • Use this code:

    open: function() {
            $(this).closest('.ui-dialog').find('.ui-dialog-buttonpane button:eq
    (1)').focus();
            $(this).closest('.ui-dialog').find('.ui-dialog-buttonpane button:eq
    (0)').blur();
    },