Open In App

How to Create Customizable Alerts in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

Alerts in JavaScript are an important components in web design. They are commonly used to notify users. The SweetAlert2 library aims to make basic looking alerts much more attractive and also provide context to the alert. The documentation and usage examples of SweetAlert2 could be found here.

Installation: The SweetAlert2 library can be included by using the following CDN link:

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>

Example 1:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Sweet Alert2</title>
  
    <!-- Include the library -->
    <script src=
    </script>
</head>
  
<body>
    <h1>
        Sweet Alert2 HTML Page
    </h1>
      
    <script type="text/javascript">
  
        // Make a simple alert
        // with the given text
        Swal.fire(
            'Hey!',
            'Welcome to GeeksForGeeks',
            'success'
        );
    </script>
</body>
  
</html>


Output:

Example 2: This example shows the use of this library for a like button.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Sweet Alert3</title>
  
    <!-- Include the library -->
    <script src=
    </script>
  
    <link rel="stylesheet" href=
</head>
  
<body>
    <h1>
        Sweet Alert3 HTML Page
    </h1>
  
    <script type="text/javascript">
  
        // Show a more complex alert with
        // the given text and properties
        Swal.fire({
  
            // Specify the title
            // of the alert
            title:
'<strong>Hit the Like Button at GeeksForGeeks</strong>',
            html: '',
  
            // Show a close button
            showCloseButton: true,
            focusConfirm: false,
  
            // Specify the text 
            // for the button
            confirmButtonText:
'<i class="fa fa-thumbs-up"></i> Great!',
            confirmButtonAriaLabel: 
                'Thumbs up, great!',
        })
    </script>
</body>
  
</html>


Output:



Last Updated : 25 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads