Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In your code, I am not seeing an event listener. Use keyup to verify that the value of the elemnt is empty.

var copyText = document.getElementById("numero");

copyText.addEventListener('keyup', () => {
  if (document.getElementById("numero").value == "") {
    Swal.fire({
      icon: 'error',
      title: 'Oops...',
      text: 'Nothing to copy!'
    })
  }
})
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <title>Document</title>

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

  <body>
    <input type="text" id="numero">
  </body>

</html>

about 4 years ago · Juan Pablo Isaza Report

0

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 😉

about 4 years ago · Juan Pablo Isaza Report

0

First of all, make sure your imports are correct. You can try debugging by using something simple like: swal("Hello world") anywhere in your code. I roughly copied yours and used a different CDN import from the Swal documentation on their website: <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

In your Javascript code, you're missing a } at the end of your function. Might also be a miscopy, but never too sure.

Also, if you want to make sure your if statement is correct, you can try debugging with console.log("Hello world") at every fork in your code. Then, open the console in your browser to see what is logged. This will help you figure out whether or not your code is running when you want it to.

Also, watch out when you use document.getElementById("numero").value. For instance, your code wouldn't work with <p> because <p> has no value, but it would with <input> or <option>.

Here is a sample of code that is working on my end:

function myFunction() {
    var copyText = document.getElementById("numero");

    if (document.getElementById("numero").innerHTML == "") {
        swal({
            icon: 'error',
            title: 'Oops...',
            text: 'Nothing to copy!'        
        })     
    } else {
        swal("Copied!")     
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!