Skip to content

Commit e4e5c29

Browse files
committed
Dialog: Added "focusSelector" option which will be used to focus the desired element when the dialog is opened. Fixed #4731 - Add option to set which element gains focus on open.
1 parent ab027a0 commit e4e5c29

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

ui/jquery.ui.dialog.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ $.widget("ui.dialog", {
4646
title: '',
4747
width: 300,
4848
zIndex: 1000,
49+
focusSelector: null,
4950
focusFilter: ''
5051
},
5152
_create: function() {
@@ -307,20 +308,24 @@ $.widget("ui.dialog", {
307308

308309
// set focus to the first tabbable element in the content area or the first button
309310
// if there are no tabbable elements, set focus on the dialog itself
310-
var arrTab = [],
311-
$tab;
311+
if (!options.focusSelector) {
312+
var arrTab = [],
313+
$tab;
314+
315+
arrTab.push(uiDialog.find('.ui-dialog-content :tabbable:not(' + options.focusFilter + '):first').filter(':not(.close)'));
316+
arrTab.push(uiDialog.find('.ui-dialog-buttonpane :tabbable:first'));
317+
arrTab.push(uiDialog);
312318

313-
arrTab.push(uiDialog.find('.ui-dialog-content :tabbable:not(' + options.focusFilter + '):first').filter(':not(.close)'));
314-
arrTab.push(uiDialog.find('.ui-dialog-buttonpane :tabbable:first'));
315-
arrTab.push(uiDialog);
316-
317-
// Focus first populated selection
318-
for (var i = 0; i < arrTab.length; i++) {
319-
$tab = arrTab[i];
320-
if ($tab.length) {
321-
$tab.focus();
322-
break;
319+
// Focus first populated selection
320+
for (var i = 0; i < arrTab.length; i++) {
321+
$tab = arrTab[i];
322+
if ($tab.length) {
323+
$tab.focus();
324+
break;
325+
}
323326
}
327+
} else {
328+
uiDialog.find(options.focusSelector).focus();
324329
}
325330

326331
self._trigger('open');

0 commit comments

Comments
 (0)