Sweet Alert 2 - Link in "confirmButtonText" button
Asked Answered
P

5

6

I would like to know how I could put a link in the button that generates the confirmButtonText of Sweet Alert 2.

The goal is that when you press that button redirects to a page that removes a record from a database, until now I had it with a simple link (<a>) in a simple button, but I would like to add this small confirmation.

Here is the code:

The code

The buttons is as follows:

The button

Prier answered 25/2, 2019 at 2:16 Comment(2)
please code as text, not imagesAlvira
User .then to move user to another page on confirmation sweetalert.js.org/guidesTyrr
A
14

This will work!

confirmButtonText: '<a href="url">LINK</a>'
Agoraphobia answered 13/4, 2019 at 15:41 Comment(2)
The problem here is that only the text will be clickable and not the full button. I find the answer of @iJassar solves this problem.Gladiolus
Additionally, this results in invalid HTML with an <a> tag nested within a <button> tag. This should not be the accepted answer.Throstle
E
6

What I did is attach a function to the confirm button that uses window.href to follow the link:

Swal.fire({
   title: '<strong>Are you sure?</strong>',
   icon: 'warning',
   html:`You really can't go back after this, We can't retrieve it either!`,
   showCloseButton: true,
   showCancelButton: true,
   focusConfirm: false,
   reverseButtons: true,
   focusCancel: true,
   cancelButtonText:`Blue pill`,
   confirmButtonText:`Red pill`
 }).then((result) => {
   if (result.value) {
     window.location.href = `/real_world`
   }
 }); 
Estreat answered 4/4, 2020 at 20:56 Comment(0)
T
3

i think i am too late to respond the query, but it might help others who searching for similar thing.

Swal.fire({
        title: '<strong>Check Redirect!!</strong>',
        icon: 'success',
        showConfirmButton: false,
        allowOutsideClick: false,
        footer:`<a class="btn btn-primary" href="https://www.google.com">OK</a>`
    });
Tyranny answered 2/9, 2020 at 16:19 Comment(1)
Clean answer. I believe this better uses the APIs of library instead of "hacking" button HTML or using nasty callbacks with window.location.Jackijackie
K
2

You can use promising in sweet alert2

Swal.fire({
 title: 'Are you sure?',
 text: "You won't be able to revert this!",
 type: 'warning',
 showCancelButton: true,
 confirmButtonColor: '#3085d6',
 cancelButtonColor: '#d33',
 confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  if (result.value) {
  Swal.fire(
   'Deleted!',
   'Your file has been deleted.',
   'success'
 )
}
})

Even more, check: https://sweetalert2.github.io/

Kaifeng answered 25/2, 2019 at 2:32 Comment(0)
E
1

Use window.open

Swal.fire({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  icon: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!',
  cancelButtonText: 'No'
}).then((result) => {
  if (result.isConfirmed) {
     window.open("https://www.google.com.br");
  }
})
Editorial answered 10/12, 2021 at 23:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.