Remove "OK" button from sweet alert dialog
Asked Answered
B

12

43

I am using javascript sweetalert2 library.

I want to remove the OK button from the alert box but I did not find any property for not to display this button.

I am using the timer property timer:1000 for closing the alert in one second. So, I don't think there is a use of the ok button in this matter.

enter image description here

Bison answered 23/2, 2017 at 5:49 Comment(2)
set showConfirmButton:false in your configuration. Link to the DocumentationEanes
swal({ content: 'Hello Test', button: false, })Valentijn
D
74

You can use these properties:

showCancelButton: false, // There won't be any cancel button
showConfirmButton: false // There won't be any confirm button

Like This

swal({
  title: 'Auto close alert!',
  text: 'I will close in 2 seconds.',
  timer: 2000,
  showCancelButton: false,
  showConfirmButton: false
}).then(
  function () {},
  // handling the promise rejection
  function (dismiss) {
    if (dismiss === 'timer') {
      //console.log('I was closed by the timer')
    }
  }
)
Deaminate answered 23/2, 2017 at 5:57 Comment(1)
Worked with my app: angular2 + sweetalert2, saved my time! :)Navarino
M
30

Update 4/6/2018

showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons, or buttons: false to hide all buttons. By default, only the confirm button is shown.

So now instead of doing

showCancelButton: false;

showConfirmButton: false;

Just do

buttons: false;

Guides

Moises answered 6/4, 2018 at 15:0 Comment(1)
This is the best answer!Dixon
H
6

You need to set showConfirmButton:false in your configuration.

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showConfirmButton:false,
  confirmButtonText: 'Yes, delete it!'
})

Here's the fiddle

Hufford answered 23/2, 2017 at 5:58 Comment(0)
W
4

Use closeOnClickOutside: false It works for me.

swal({      
    title: "Success",
    text: "Permissions assigned Successfully",
    icon: "success",
    closeOnClickOutside: false,
})
Welton answered 28/5, 2018 at 6:31 Comment(0)
V
4

One more way to do the same.

Swal.fire({
  type: 'error',
  title: 'Cancelled',
  text: 'Your offer is safe 🙂',
  showConfirmButton: false,
  timer: 2000
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>

Upgrading to v9.x of SweetAlert2.

Breaking change - rename type to icon

Swal.fire({
  icon: 'error',
  title: 'Cancelled',
  text: 'Your offer is safe 🙂',
  showConfirmButton: false,
  timer: 2000
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
Ventose answered 30/9, 2019 at 15:19 Comment(0)
G
4

SWEET ALERT 2 | 2022 UPDATE

Swal.fire({
    ...
    showConfirmButton: false, //hide OK button
    allowOutsideClick: false, //optional, disable outside click for close the modal
    ...
});

As new documentation here.

Gallopade answered 11/2, 2022 at 7:41 Comment(0)
M
3

This works for me: $(".confirm").attr('disabled', 'disabled');

My function:

function DeleteConfirm(c){
  swal({   
            title: "Want to delete this item?",   
            text: "You will not be able to undo this action!",   
            type: "warning",   
            showCancelButton: true,   
            confirmButtonColor: "#DD6B55",   
            confirmButtonText: "Yes, delete it!",   
            closeOnConfirm: false 
        }, function(){ 
          $(".confirm").attr('disabled', 'disabled'); 

        });
}
Merthiolate answered 1/11, 2017 at 15:34 Comment(0)
P
2

This accepted answer has been deprecated. Here is how you can hide or remove buttons in SweetAlert2.

{
  buttons: false,
}
Palembang answered 1/5, 2020 at 3:22 Comment(0)
J
0

Try setting the showConfirmButton property to false.

Look at their docs

Jolly answered 23/2, 2017 at 5:55 Comment(0)
A
0

Before adding any buttons,clear all the buttons and then re-add them like(assuming the Alert name is 'A') -

A.getButtonTypes().clear();
ButtonType OpenStorage=new ButtonType("Open Storage");
A.getButtonTypes().addAll(OpenStorage,ButtonType.CANCEL,ButtonType.NEXT);

Hope it'll help!!!

Arakawa answered 13/5, 2017 at 15:25 Comment(0)
N
0

Below code works for me

I have only set buttons: false;

and update

swal({
    title: 'Auto close alert!',
    text: 'I will close in 2 seconds.',
    timer: 2000,
    showCancelButton: false,
    showConfirmButton: false
});
Nicoline answered 5/9, 2018 at 11:36 Comment(0)
S
0

Header Part:

<link rel="stylesheet" href="https://sweetalert2.github.io/styles/bootstrap4-buttons.css">

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

Body Part Add A Button:

<a href="" id="delete" class="btn btn-danger">Delete</a>

jQuery Part:

$(document).on("click", "#delete", function (e) {
        e.preventDefault();
        var link = $(this).attr("href"); const swalWithBootstrapButtons = swal.mixin({   customClass: {
    confirmButton: 'btn btn-success',
    cancelButton: 'btn btn-danger'   },   buttonsStyling: false }) swalWithBootstrapButtons.fire({   title: 'Are you sure?',   text: "You won't be able to revert this!",   icon: 'warning',   showCancelButton: true,   confirmButtonText: 'Yes, delete it!',   cancelButtonText: 'No, cancel!',   reverseButtons: true }).then((result) => {   if (result.isConfirmed) {
    swalWithBootstrapButtons.fire(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    )   } else if (
    /* Read more about handling dismissals below */
    result.dismiss === swal.DismissReason.cancel   ) {
    swalWithBootstrapButtons.fire(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    )   } }) });
Silvern answered 18/6, 2021 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.