I’am trying (without luck) to implement a Swal
inside a If statement.
here is what i’ve done:
JavaScript
x
15
15
1
function myFunction() {
2
/* Get the text field */
3
var copyText = document.getElementById("numero");
4
5
//check if try to copy with the empty input
6
if(document.getElementById("numero").value == ""){
7
Swal.fire({
8
icon: 'error',
9
title: 'Oops...',
10
text: 'Nothing to copy!'
11
})
12
// alert("Nothing to copy!!")
13
} else {
14
15
}
And here is the links in my html:
JavaScript
1
8
1
<!-- dark theme for swal -->
2
<link href="//cdn.jsdelivr.net/npm/@sweetalert2/theme-dark@4/dark.css" rel="stylesheet">
3
4
<!-- javascript file -->
5
<script src="app.js"></script>
6
7
<!-- swal link -->
8
<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:
JavaScript
1
16
16
1
function myFunction() {
2
/* Get the text field */
3
var copyText = document.getElementById("numero");
4
5
if(document.getElementById("numero").value == ""){
6
7
Swal.fire(
8
'Ooops...',
9
'Nothing to copy here!',
10
'error'
11
)
12
event.preventDefault()
13
14
} else {}
15
/*code here
16
}
Thanks for giving me that tips guys 😉