Configuration
-
text
Type:
string
Default:
""
(empty string)Description:
The modal's text. It can either be added as a configuration under the key
text
(as in the example below), or passed as the first and only parameter (e.g.swal("Hello world!")
), or the second one if you have multiple string parameters (e.g.swal("A title", "Hello world!")
).Example:
;
-
title
Type:
string
Default:
""
(empty string)Description:
The title of the modal. It can either be added as a configuration under the key
title
(as in the example below), or passed as the first string parameter – as long as it's not the only one – of theswal
function (e.g.swal("Here's a title!", "Here's some text")
).Example:
;
-
icon
Type:
string
Default:
""
(empty string)Description:
An icon for the modal. SweetAlert comes with 4 built-in icons that you can use:
"warning"
"error"
"success"
"info"
It can either be added as a configuration under the key
icon
, or passed as the third string parameter of theswal
function (e.g.swal("Title", "Text", "success")
).Example:
;
-
button
Type:
string|boolean|ButtonOptions
Default:
text: "OK"value: truevisible: trueclassName: ""closeModal: trueDescription:
The confirm button that's shown by default. You can change its text by setting
button
to a string, or you can tweak more setting by passing aButtonOptions
object. Setting it tofalse
hides the button.Examples:
;;;
-
buttons
Type:
boolean|string[]|ButtonOptions[]|ButtonList
Default:
cancel:text: "Cancel"value: nullvisible: falseclassName: ""closeModal: trueconfirm:text: "OK"value: truevisible: trueclassName: ""closeModal: trueDescription:
Specify the exact amount of buttons and their behaviour. If you use an array, you can set the elements as strings (to set only the text), a list of
ButtonOptions
, or a combination of both. You can also set one of the elements totrue
to simply get the default options.If you want more than just the predefined cancel and confirm buttons, you need to specify a
ButtonList
object, with keys (the button's namespace) pointing toButtonOptions
.You can also specify
false
to hide all buttons (same behaviour as thebutton
option).Examples:
;;;;;
-
content
Type:
Node|string
Default:
null
Description:
For custom content, beyond just text and icons.
Examples:
;;var slider = document;slidertype = "range";;
-
className
Type:
string
Default:
""
(empty string)Description:
Add a custom class to the SweetAlert modal. This is handy for changing the appearance.
Example:
; -
closeOnClickOutside
Type:
boolean
Default:
true
Description:
Decide whether the user should be able to dismiss the modal by clicking outside of it, or not.
Example:
; -
closeOnEsc
Type:
boolean
Default:
true
Description:
Decide whether the user should be able to dismiss the modal by hitting the ESC key, or not.
Example:
;
-
dangerMode
Type:
boolean
Default:
false
Description:
If set to
true
, the confirm button turns red and the default focus is set on the cancel button instead. This is handy when showing warning modals where the confirm action is dangerous (e.g. deleting an item).Example:
;
-
timer
Type:
number
Default:
null
Description:
Closes the modal after a certain amount of time (specified in ms). Useful to combine with
buttons: false
.Example:
;
Methods
Name | Description | Example |
---|---|---|
close |
Closes the currently open SweetAlert, as if you pressed the cancel button. | swal.close() |
getState |
Get the state of the current SweetAlert modal. | swal.getState() |
setActionValue |
Change the promised value of one of the modal's buttons. You can either pass in just a string (by default it changes the value of the confirm button), or an object. | swal.setActionValue({ confirm: 'Text from input' }) |
stopLoading |
Removes all loading states on the modal's buttons. Use it in combination with the button option closeModal: false . |
swal.stopLoading() |
Theming
-
swal-overlay
Example:
-
swal-modal
Example:
-
swal-title
Example:
-
swal-text
Example:
-
swal-footer
Example:
-
swal-button
Description:
The modal's button(s). It has an extra class that changes depending on the button's type, in the format
swal-button--{type}
. The extra class for the confirm button for example isswal-button--confirm
.Example: