t4t5 / sweetalert

A beautiful replacement for JavaScript's "alert"
https://sweetalert.js.org
MIT License
22.4k stars 2.84k forks source link

How to remove the auto-focus onOpen? #849

Open sentiasa opened 6 years ago

sentiasa commented 6 years ago

Is there a way to remove auto-focus after initial opening, so it doesn't highlight/focus any of the buttons?

shangxinbo commented 6 years ago

same issue

wesleylhandy commented 5 years ago

I wanted to add focus to the first button, rather than the last. I'm using jQuery, so I added the following code:

function transferFocus(e) {
    $('.swal-button').blur();
    $('.swal-button--confirm').focus();
}
$(document).on('focus', '.swal-button--cancel', transferFocus);
$(document).on('focus', '.swal-button--confirm', function(e) {
    $(document).off('focus', '.swal-button--cancel', transferFocus);
});

You can take the same principles and use vanilla Javascript to prevent event delegation on the focus event for your alerts.

t4t5 commented 5 years ago

You can switch the button focus to the other button by using the dangerMode attribute (https://sweetalert.js.org/docs/#dangermode). There's no special option to disable all focus though unfortunately.

Maybe we should provide an onOpen method so that people can manually manipulate the DOM when calling certain swals?

Androz2091 commented 3 years ago

Yes this is a good idea