Whenever I type something in the following Insert Hyperlink text input, all the words are going to textarea
behind it. OK and Cancel buttons are working fine but I cannot focus to text input.
We'are using jQuery UI 1.10.1. It was working nicely with previous version of jQuery which was 1.8.x.
I've checked code behind of jQuery and it has the following methods called when opening a Modal Dialog:
_focusTabbable: function () {
// Set focus to the first match:
// 1. First element inside the dialog matching [autofocus]
// 2. Tabbable element inside the content element
// 3. Tabbable element inside the buttonpane
// 4. The close button
// 5. The dialog itself
var hasFocus = this.element.find("[autofocus]");
if (!hasFocus.length) {
hasFocus = this.element.find(":tabbable");
}
if (!hasFocus.length) {
hasFocus = this.uiDialogButtonPane.find(":tabbable");
}
if (!hasFocus.length) {
hasFocus = this.uiDialogTitlebarClose.filter(":tabbable");
}
if (!hasFocus.length) {
hasFocus = this.uiDialog;
}
hasFocus.eq(0).focus();
},
_keepFocus: function (event) {
function checkFocus() {
var activeElement = this.document[0].activeElement,
isActive = this.uiDialog[0] === activeElement ||
$.contains(this.uiDialog[0], activeElement);
if (!isActive) {
this._focusTabbable();
}
}
event.preventDefault();
checkFocus.call(this);
// support: IE
// IE <= 8 doesn't prevent moving focus even with event.preventDefault()
// so we check again later
this._delay(checkFocus);
},
that is taken from here: http://code.jquery.com/ui/1.10.1/jquery-ui.js
I had a similar issue where I needed the focus to be within the content of my dialog box (for WCAG). Using focus() alone failed, so what my end solution was in the dialog instantiation I added:
I used the timeout to ensure compatibility. *Note, I made the '#element' an anchor tag (interactive element) so the focus would take. That is the reason for the styling.
This code should be able to be added into the "open" function of the jQuery Dialog as well.
Another way to stop blocking focus from jquery dialog
Where
.input-container
is container which contains controls that should receive focus.Second answer I've found is that in the following code jQuery binds document to dialog. So when I unbind this when I click on the desired button's onclick event (or whatever event you're handling):
This is where jQuery UI binds it
_focusTabble()
method tofocusin.dialog
event of document.thats because jquery prevents focus outside of dialog child, jquery has this method you can read, that will whitelist which other elements you want to allow focus.
https://api.jqueryui.com/dialog/#method-_allowInteraction
So what i do to disable this "block focus" to some items with class .other-popups is add this line in the code
Or to disable completly
What I did to solve this problem is to comment out this
$(".ui-dialog:visible:last .ui-dialog-content").data("ui-dialog")._focusTabbable();
You can find the complete code below: