How to remove class from button and add custom class in sweetalert2?
Asked Answered
G

2

6

I am using sweetalert2 plugin. It works fine, but now i want to remove the button class and add my own class.

class=swal2-confirm swal2-styled but i need class=swal2-confirm btn btn-success

Any solution appreciated!

 Swal.fire({
        title: 'Are you sure?',
        text: 'You will not be able to recover this imaginary file!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes, delete it!',
        cancelButtonText: 'No, keep it,'
    }).then((result) => {
        if (result.value) {
            Swal.fire(
                'Deleted!',
                'Your imaginary file has been deleted.',
                'success'
            )

        } else if (result.dismiss === Swal.DismissReason.cancel) {
            Swal.fire(
                'Cancelled',
                'Your imaginary file is safe :)',
                'error'
            )
        }
    })
Goran answered 13/7, 2019 at 12:59 Comment(0)
I
10

You can do it using customClass:

customClass: {
    confirmButton: 'my-confirm-button-class'
},

Or, you may toggle classes onBeforeOpen:

onBeforeOpen: function(ele) {
    $(ele).find('button.swal2-confirm.swal2-styled')
          .toggleClass('swal2-confirm swal2-styled swal2-confirm btn btn-success')
}

$('#btn').on('click', function (e) {
  Swal.fire({
      title: 'Are you sure?',
      text: 'You will not be able to recover this imaginary file!',
      type: 'warning',
      showCancelButton: true,
      confirmButtonText: 'Yes, delete it!',
      cancelButtonText: 'No, keep it,',
      customClass: {
          //confirmButton: 'my-confirm-button-class'
      },
      onBeforeOpen: function(ele) {
          $(ele).find('button.swal2-confirm.swal2-styled').toggleClass('swal2-confirm swal2-styled swal2-confirm btn btn-success')
      }
  }).then(function (result) {
      if (result.value) {
          Swal.fire(
                  'Deleted!',
                  'Your imaginary file has been deleted.',
                  'success'
          )

      } else if (result.dismiss === Swal.DismissReason.cancel) {
          Swal.fire(
                  'Cancelled',
                  'Your imaginary file is safe :)',
                  'error'
          )
      }
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.css">


<button id="btn">Click Me</button>

is there a way where i can change permanent classes

I would suggest to add your local css style:

.swal2-styled.swal2-confirm {
.....
}

$('#btn').on('click', function (e) {
    Swal.fire({
        title: 'Are you sure?',
        text: 'You will not be able to recover this imaginary file!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes, delete it!',
        cancelButtonText: 'No, keep it,'
    }).then(function (result) {
        if (result.value) {
            Swal.fire(
                    'Deleted!',
                    'Your imaginary file has been deleted.',
                    'success'
            )

        } else if (result.dismiss === Swal.DismissReason.cancel) {
            Swal.fire(
                    'Cancelled',
                    'Your imaginary file is safe :)',
                    'error'
            )
        }
    })
})
.swal2-styled.swal2-confirm {
    color: #fff !important;
    background-color: #28a745 !important;
    border-color: #28a745 !important;
    display: inline-block !important;
    font-weight: 400 !important;
    color: #212529 !important;
    text-align: center !important;
    vertical-align: middle !important;
    -webkit-user-select: none !important;
    -moz-user-select: none !important;
    -ms-user-select: none !important;
    user-select: none !important;
    background-color: transparent !important;
    border: 1px solid transparent !important;
    padding: 0.375rem 0.75rem !important;
    font-size: 1rem !important;
    line-height: 1.5 !important;
    border-radius: 0.25rem !important;
    transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.css">


<button id="btn">Click Me</button>
Immolate answered 13/7, 2019 at 13:41 Comment(1)
I up-arrowed the answer but not the question as I wasn't trying to change the class (no offense to the question though, one day I'll need to do that I wonder!). I had a CSS problem so went for the CSS solution - and I had everything except I just forgot the !important :)Apt
S
5

You can do it using:

  buttonsStyling: false,
  customClass: {
         confirmButton: "btn btn-outline-danger"
       }
Storyteller answered 12/2, 2021 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.