blur
blur

Reputation: 89

Autofocus only after sweet alert is closed

How can i focus on a textbox in my page only after the sweet alert has been closed. this is the code i am using

swal({
title:'Student Already Logged In!', 
text: 'Student already logged in.', 
type: 'warning', 
timer: 2500, 
showConfirmButton: false
});

Upvotes: 1

Views: 3078

Answers (1)

Fcmam5
Fcmam5

Reputation: 6922

If you're using SweetAlert2, you need to add a promise with a myElement.focus(); as it's explained in the documentation.

For example your textBox has id='my-input' the JS code you need is:

(function() {

  swal({
    title: 'Student Already Logged In!',
    text: 'Student already logged in.',
    type: 'warning',
    timer: 2500,
    showConfirmButton: false
  }).then(function() {
    document.getElementById('my-input').focus();
  });

})()
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>

<input type="text" id="my-input">

Or if you are using SweetAlert (v1.x) you need to pass a callback as a second parameter of swal().

swal({
  title: 'Student Already Logged In!',
  text: 'Student already logged in.',
  type: 'warning',
  timer: 2500,
  showConfirmButton: false
}, function() {
  document.getElementById('my-input').focus();
});

Upvotes: 1

Related Questions