Reputation: 3783
I'm using sweetAlert this is working fine with single button but now i want to click button with choice but I'm not able to that, What is wrong?
Now my redirection is working on both buttons but i want to click only for single button. How can i do that?
What i tried:-
function CallMessage() {
swal({
title: 'this is title',
text: 'message',
buttons: ["Finish Now", "Exit Anyway"]
}).then(function() {
window.location = 'login.aspx';
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<button onclick="CallMessage();">Button</button>
Answer will be appriciated!
Upvotes: 0
Views: 40
Reputation: 6145
The promise returned by swal()
is resolved to the value of the clicked button. When using the array shorthand to set the buttons
property, the first (cancel) button returns null
, while the rest return true
:
function CallMessage() {
swal({
title: 'this is title',
text: 'message',
buttons: [ 'Finish Now', 'Exit Anyway' ]
}).then(value => {
console.log(value);
if (value) {
console.log('Redirecting!');
// location.href = 'login.aspx';
}
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<button onclick="CallMessage();">Button</button>
If you have more than two buttons, you can also set custom values for each one using an object instead of an array. See the Advanced Examples section of the docs for more details.
Upvotes: 2