{"id":1684,"date":"2024-02-26T05:26:03","date_gmt":"2024-02-26T05:26:03","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1684"},"modified":"2024-02-26T05:26:06","modified_gmt":"2024-02-26T05:26:06","slug":"how-to-use-sweetalert2","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/how-to-use-sweetalert2\/","title":{"rendered":"How to use SweetAlert2"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"427\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/02\/image-68.png\" alt=\"\" class=\"wp-image-1685\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/02\/image-68.png 640w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/02\/image-68-300x200.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/02\/image-68-400x266.png 400w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>SweetAlert2 is a beautiful, responsive, and customizable replacement for JavaScript&#8217;s native alert, confirm, and prompt dialogs. It&#8217;s built to be flexible and easy to use, allowing developers to create visually appealing alert messages that seamlessly integrate into their web applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Choose SweetAlert2?<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Customization<\/strong>: SweetAlert2 provides extensive customization options, allowing developers to tailor alert messages to match their application&#8217;s design language perfectly.<\/li>\n\n\n\n<li><strong>Responsive Design<\/strong>: The alerts generated by SweetAlert2 are responsive out of the box, ensuring a consistent user experience across devices and screen sizes.<\/li>\n\n\n\n<li><strong>Ease of Use<\/strong>: Integrating SweetAlert2 into your project is straightforward, with clear and concise documentation available to guide you through the process.<\/li>\n\n\n\n<li><strong>Animations<\/strong>: With built-in animations, SweetAlert2 adds flair to your alert messages, making them more engaging and visually appealing.<\/li>\n<\/ol>\n\n\n\n<p>Integrating SweetAlert2 into your web application is a breeze. Here&#8217;s a quick guide to get you started:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Installation<\/strong>: You can include SweetAlert2 in your project by adding the script tags directly to your HTML file or by installing it via package managers like npm or yarn.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- Add this to your HTML file -->\r\n&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/sweetalert2@11\">&lt;\/script>\r\n<\/code><\/pre>\n\n\n\n<p><strong>Basic Usage<\/strong>: Once included, you can use SweetAlert2 to display alerts, confirms, and prompts with just a few lines of code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Simple alert\r\nSwal.fire('Hello, world!')\r\n\r\n\/\/ Confirm dialog\r\nSwal.fire({\r\n  title: 'Are you sure?',\r\n  text: \"You won't be able to revert this!\",\r\n  icon: 'warning',\r\n  showCancelButton: true,\r\n  confirmButtonColor: '#3085d6',\r\n  cancelButtonColor: '#d33',\r\n  confirmButtonText: 'Yes, delete it!'\r\n}).then((result) => {\r\n  if (result.isConfirmed) {\r\n    Swal.fire(\r\n      'Deleted!',\r\n      'Your file has been deleted.',\r\n      'success'\r\n    )\r\n  }\r\n})\r\n<\/code><\/pre>\n\n\n\n<p><strong>Customization<\/strong>: SweetAlert2 allows you to customize every aspect of your alert messages, including titles, texts, icons, buttons, and animations. Refer to the documentation for a comprehensive list of customization options.<\/p>\n\n\n\n<p>Example of how you can integrate SweetAlert2 into an HTML file to showcase its usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\r\n&lt;html lang=\"en\">\r\n&lt;head>\r\n  &lt;meta charset=\"UTF-8\">\r\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\r\n  &lt;title>SweetAlert2 Example&lt;\/title>\r\n  &lt;!-- Include SweetAlert2 CSS -->\r\n  &lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/sweetalert2@11\/dist\/sweetalert2.min.css\">\r\n&lt;\/head>\r\n&lt;body>\r\n  &lt;h1>SweetAlert2 Example&lt;\/h1>\r\n  &lt;button onclick=\"showAlert()\">Show Alert&lt;\/button>\r\n  &lt;button onclick=\"showConfirm()\">Show Confirm&lt;\/button>\r\n\r\n  &lt;!-- Include SweetAlert2 JS -->\r\n  &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/sweetalert2@11\">&lt;\/script>\r\n  &lt;script>\r\n    \/\/ Function to show a simple alert\r\n    function showAlert() {\r\n      Swal.fire('Hello, world!');\r\n    }\r\n\r\n    \/\/ Function to show a confirm dialog\r\n    function showConfirm() {\r\n      Swal.fire({\r\n        title: 'Are you sure?',\r\n        text: \"You won't be able to revert this!\",\r\n        icon: 'warning',\r\n        showCancelButton: true,\r\n        confirmButtonColor: '#3085d6',\r\n        cancelButtonColor: '#d33',\r\n        confirmButtonText: 'Yes, delete it!'\r\n      }).then((result) => {\r\n        if (result.isConfirmed) {\r\n          Swal.fire(\r\n            'Deleted!',\r\n            'Your file has been deleted.',\r\n            'success'\r\n          )\r\n        }\r\n      })\r\n    }\r\n  &lt;\/script>\r\n&lt;\/body>\r\n&lt;\/html>\r\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We include the SweetAlert2 CSS file via a <code>&lt;link&gt;<\/code> tag in the <code>&lt;head&gt;<\/code> section.<\/li>\n\n\n\n<li>We include the SweetAlert2 JavaScript file via a <code>&lt;script&gt;<\/code> tag just before the closing <code>&lt;\/body&gt;<\/code> tag.<\/li>\n\n\n\n<li>Two buttons are provided to demonstrate the usage of SweetAlert2: one for showing a simple alert and another for showing a confirmation dialog.<\/li>\n\n\n\n<li>JavaScript functions <code>showAlert()<\/code> and <code>showConfirm()<\/code> are defined to trigger the respective SweetAlert2 messages.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SweetAlert2 is a beautiful, responsive, and customizable replacement for JavaScript&#8217;s native alert, confirm, and prompt dialogs. It&#8217;s built to be flexible and easy to use, allowing developers&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1122,1134,1132,1126,1128,45,1129,1123,1133,1124,1125,1131,1127,675,1130],"class_list":["post-1684","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-alert-messages","tag-code-snippets","tag-customization","tag-frontend-development","tag-interactive-design","tag-javascript","tag-responsive-design","tag-sweetalert2","tag-ui-components","tag-ui-ux","tag-user-experience","tag-web-app-development","tag-web-design","tag-web-development","tag-web-development-tips"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1684"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1684\/revisions"}],"predecessor-version":[{"id":1686,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1684\/revisions\/1686"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}