Search code examples
javascriptsweetalertsweetalert2

How to change SweetAlert keyword from `swal` to 'swal2'


Hi I've been using SweetAlert and I need the SweetAlert2 chaining modals.

I don't want to remove SweetAlert because it is used vastly in our application. How can I change the keyword swal of SweetAlert2 to swal2 so that it will not collide with our current SweetAlert plugin?

So that I can have:

swal2('Sweet Alert 2');

Solution

  • Here's what I did and it works:

    <link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet"/>
    
    <button id="swal">swal</button>
    <button id="swal2">swal2</button>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="https://unpkg.com/sweetalert2@latest/dist/sweetalert2.all.js"></script>
    <script>
        var swal2 = swal;
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
    <script>
        $('#swal').on('click', function() { swal('Hi from swal', 'success') })
        $('#swal2').on('click', function() { swal2('Hi from swal2') })
    </script>