Home > OS >  Can't focus input when jquery confirm onClose event
Can't focus input when jquery confirm onClose event

Time:01-31

I need to focus node when onClose event. but always focused clicked button.

        $.alert({
            title: 'title',
            content: 'message,
            onClose : function () {
                $('input[name="title"]').focus();
            }
        });

CodePudding user response:

By default jQuery-confirm keeps track of the last focused element when it is opened, and when it is closed, it returns the last focused element to focus.

So work around for that:

Example:

$.alert({
  title: 'title',
  content: 'message',
  onClose: function() {
    this._lastFocused = $('input[name="title"]');
    // this will notify jquery-confirm to focus the  element when it close.
  }
});

CodePudding user response:

event being fired before the dialog is fully closed. To resolve this, you can use the setTimeout function to delay the focus operation until the dialog is fully closed:

$("#confirm").on("confirm:close", function () {
      setTimeout(function () {
        $("#input").focus();
      }, 200);
    });
  • Related