How do you implement a sweet alert?

In this blog we will create live example for sweet alert. Sweet Alert is a method to customize alerts in your applications, websites and games. A beautiful, responsive, customizable and accessible replacement for JavaScript’s popup boxes.

sweet alert box

What is a sweet alert?

Sweet Alert is a way to customize alerts in your games and websites. It allows you to change from a standard JavaScript button. We can add buttons, change the color text, and even add additional alerts that change depending on user click.

Let’s start step by step configuration and create live example to understand how sweet alert work.

Step 1: Create one HTML file and include below library.

<script src="" ></script>
<script src="" ></script>

Step 2: Create one simple button and call one function with onclick method like onclick="function1()"

<button type="button" class="btn btn-primary" onclick="function1()">Basic message</button>

Step 3: Finally you need to call your sweet alert message with below code.

<script language="JavaScript">
function function1() { 
	swal("Hello world!");
simple alert message

So this is how you can simply configure sweet alert. Now let’s see some another examples of sweetAlert.

Create sweet alert with title and text

<button type="button" class="btn btn-secondary" onclick="function2()">Title with a text</button>

<script language="JavaScript">
function function2() { 
swal("Success!", "Your data have been saved. Thank you!");

Create sweet alert with title and text

In above example you need to pass two arguments and one is for title and second as a text.

Create success and error icon in sweet alert with title and text

<button type="button" class="btn btn-success" onclick="function3()">Success with icon</button>
<button type="button" class="btn btn-danger" onclick="function4()">Error with icon</button>

<script language="JavaScript">
function function3() { 
	swal("Success!", "Your data have been saved. Thank you!", "success");
function function4() { 
	swal("Sorry!", "Opps, something went wrong. Please try again later.", "error");

Create success message icon
error message in sweet alert

In above example you must have to pass third parameter as "success" and "error" to display animated icons.

Display confirm message before you delete or save something

<button type="button" class="btn btn-warning" onclick="function5()">With confirm button</button>

function function5() {
	  title: "Are you sure?",
	  text: "Once deleted, you will not be able to recover this file!",
	  icon: "warning",
	  buttons: true,
	  dangerMode: true,
	.then((willDelete) => {
	  if (willDelete) {
		swal("Poof! Your file has been deleted!", {
		  icon: "success",
	  } else {
		swal("Your file is safe!");

confirmation message before delete

 This comes in handy if you want to warn the user before they perform a dangerous action. 

A message with a custom icon or image

<button type="button" class="btn btn-info" onclick="function6()">With image</button>

function function6() {
	  title: "Sweet!",
	  text: "Here's a custom image.",
	  icon: ""

display one image with some message text

In above example we display one image with some message text.

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply