Sweetalert: input field not functional when used on top of a bootstrap modal

Created on 29 Jul 2015  ·  22Comments  ·  Source: t4t5/sweetalert

I am currently calling swal from within a bootstrap modal. It seems that the buttons work but the input of the swal is not accepting a click nor does it ever take focus. I'm wondering if this is a known issue or possibly something I'm doing.

Most helpful comment

I found that the culprit is tabindex="-1" that's set on Bootstrap's modal.

The fix:

// call this before showing SweetAlert:
function fixBootstrapModal() {
  var modalNode = document.querySelector('.modal[tabindex="-1"]');
  if (!modalNode) return;

  modalNode.removeAttribute('tabindex');
  modalNode.classList.add('js-swal-fixed');
}

// call this before hiding SweetAlert (inside done callback):
function restoreBootstrapModal() {
  var modalNode = document.querySelector('.modal.js-swal-fixed');
  if (!modalNode) return;

  modalNode.setAttribute('tabindex', '-1');
  modalNode.classList.remove('js-swal-fixed');
}

All 22 comments

I had the same issue. What I ended up doing was 'hiding' the modal with modal.hide, opening the swal, doing whatever you need with the inputValue, then show the modal with modal.show on success or cancel of the swal.
Hope this helps.

Dan
I found a fix for this. I'll post it tomorrow 

Sent from Outlook

On Mon, Aug 10, 2015 at 6:52 AM -0700, "Dan Kaufman" [email protected] wrote:

I had the same issue. What I ended up doing was 'hiding' the modal with modal.hide, opening the swal, doing whatever you need with the inputValue, then show the modal with modal.show on success or cancel of the swal.

Hope this helps.


Reply to this email directly or view it on GitHub.

Hi mteichtahl,

Did you stil post the fix? I can not find it. Would really help!

Hi,

Same issue with sweet alert (as well as sweetalert2).
The input field does not get the focus on firefox only. This happens only when opening a swal from a bootstrap modal.

Cannot figure out the problem yet. Really strange.

I have the same problem...any one have a solution?

hi, this allow to show input placeholder, but is not fix to focus on input...

#537

I have the same problem, can't find the fix...

+1

I found that the culprit is tabindex="-1" that's set on Bootstrap's modal.

The fix:

// call this before showing SweetAlert:
function fixBootstrapModal() {
  var modalNode = document.querySelector('.modal[tabindex="-1"]');
  if (!modalNode) return;

  modalNode.removeAttribute('tabindex');
  modalNode.classList.add('js-swal-fixed');
}

// call this before hiding SweetAlert (inside done callback):
function restoreBootstrapModal() {
  var modalNode = document.querySelector('.modal.js-swal-fixed');
  if (!modalNode) return;

  modalNode.setAttribute('tabindex', '-1');
  modalNode.classList.remove('js-swal-fixed');
}

I have a nearly identical issue, except it only occurs if the modal is so tall that it is scrollable on the y-axis.

Please help me...

Cinamonas's tab fix didn't work for me :(

+1
@eminumut @navas
Have you called fixBootstrapModal() before showing swal? I forgot to do that at first, but now works perfectly!

Thank you @Cinamonas

@Alagaesia93

Would you show me some examples?

@eminumut

fixBootstrapModal()
swal({whatever}, function(isConfirm){
whatever
restoreBootstrapModal()
}

It works for me :)

The fix generates a syntax error in IE11

@caleb87, I've updated the snippet not to use arrow functions and const.

This seems to be an issue with Bootstrap so I will close for now. Thanks for the fix @Cinamonas!

Hi , I have faced the same issue .
This issue can be solved by removing { tabindex="-1" } from Bootstrap Modal.
It's work fine for me.

Does anyone know how to apply this fix in R? I'm having the same issue, but my attempts to convert it to R have failed so far

I found that the culprit is tabindex="-1" that's set on Bootstrap's modal.

The fix:

// call this before showing SweetAlert:
function fixBootstrapModal() {
  var modalNode = document.querySelector('.modal[tabindex="-1"]');
  if (!modalNode) return;

  modalNode.removeAttribute('tabindex');
  modalNode.classList.add('js-swal-fixed');
}

// call this before hiding SweetAlert (inside done callback):
function restoreBootstrapModal() {
  var modalNode = document.querySelector('.modal.js-swal-fixed');
  if (!modalNode) return;

  modalNode.setAttribute('tabindex', '-1');
  modalNode.classList.remove('js-swal-fixed');
}

genius

For those who have more than one modal, you can use:
Documento sem título (1).pdf

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daftspunk picture daftspunk  ·  4Comments

rafatux picture rafatux  ·  5Comments

Untit1ed picture Untit1ed  ·  5Comments

blackrosezy picture blackrosezy  ·  6Comments

voodoo6 picture voodoo6  ·  4Comments