How to Create Customizable Alerts in JavaScript ?

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=
"https://cdn.jsdelivr.net/npm/sweetalert2@9">
    </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 w3wiki',
            '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=
"https://cdn.jsdelivr.net/npm/sweetalert2@9">
    </script>
  
    <link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</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 w3wiki</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:



Contact Us