Skip to content
Advertisement

Sweet Alert inside a If Statement

I’am trying (without luck) to implement a Swal inside a If statement.

here is what i’ve done:

function myFunction() {
    /* Get the text field */
    var copyText = document.getElementById("numero");
    
    //check if try to copy with the empty input 
    if(document.getElementById("numero").value == ""){
        Swal.fire({
            icon: 'error',
            title: 'Oops...',
            text: 'Nothing to copy!'        
          })      
        // alert("Nothing to copy!!")
    } else {
    
    }

And here is the links in my html:

  <!-- dark theme for swal -->
    <link href="//cdn.jsdelivr.net/npm/@sweetalert2/theme-dark@4/dark.css" rel="stylesheet"> 
    
    <!-- javascript file  -->
    <script src="app.js"></script>
    
    <!-- swal link -->
    <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>

Any tip to how can I make this work? I guess my problem is inside the If statement, but I don’t know how to fix it

Advertisement

Answer

end up that everything was working nice, but… I forget to put this in my code

event.preventDefault()

That prevent the Swal to not work well.

By the end, the final code went like this:

function myFunction() {
    /* Get the text field */
    var copyText = document.getElementById("numero");
 
    if(document.getElementById("numero").value == ""){

        Swal.fire(
            'Ooops...',
            'Nothing to copy here!',
            'error'          
          )
          event.preventDefault()
 
    } else {}
    /*code here
    }

Thanks for giving me that tips guys 😉

Advertisement