Hit enter after type your search item

6 Simple and beautiful JavaScript alert with demos and code

Purpose of alerts

The alerts of JavaScript are used to notify users for certain functions, warnings, dangerous action, etc. The JavaScript alerts are also used by web developers to debug or find some problem or track the values of variables etc.

In this tutorial, I will show you how to create simple alerts in JavaScript. I will also show you fancy style alerts of different types like confirm, prompt, etc. by using the third party JavaScript plug-ins.

A simple alert of JavaScript can be created quite easily, as shown below:

A demo to create and trigger simple alert

In this example, a simple alert is created by using the alert keyword. As you click the button, the alert will be shown.



JavaScript alert simple

An alert with a variable value

You can use variables inside the JavaScript alert dialogues. Simply place a variable name in place of string in double quotes and the alert will show the variable value.

This is a useful feature for developers as well for debugging applications (apart from other options, of course).

You see, I simply used variable name inside the alert without double quotes:

Similarly, you may concatenate a variable with a string by using the operator as shown below:

alert(“Alert with concatenation: ” + alertstr);

Creating fancy alerts

All the alerts in the above examples are using the default browser-based style that is considered boring and old-fashioned.

Unfortunately, this is what JavaScript comes up with. In order to use beautifully designed alert boxes or dialogue, you can use third party JavaScript solutions. For that, you simply need to include the JS libraries provided by alert developers into your web pages or follow their guidelines.

I will show you demos of fancy JavaScript alerts by using plug-ins. You may also see the steps to set up that plug-in for your web page after the demos.

A basic alert demo by using a plug-in

In this demo, I have used a plug-in, SweetAlert, which is available on the Github website. This plug-in is quite simple to use. First, have a look at a few demos of different alerts by using it.

See a basic alert demo online by using SweetAlert:

JavaScript alert

See online demo and code

The code:

You can see that instead of using “alert” keyword in the <script> section, I just used “swal” to create a basic alert with a message and “Ok” button to close it.

An alert with success and animation

This JavaScript-based alert can be useful for some successful actions by the user. For example, creating an account or subscribing for updates etc.

JavaScript alert success

The complete JS function code to create this alert:

This function is called at the click event of the button.

You may show this alert after filling out the form by the user and upon successful entry into the database.

A JavaScript confirm dialogue example

You may use this alert type to give a warning to the user before proceeding to some critical action like “deleting an account permanently” or deleting a file etc.

The code:


JavaScript confirm alert

You can see that a user is presented with a confirmation alert with Red button to proceed or cancel the operation. In each case, the related message is shown in next alert with an Ok button to close the alert. You may perform an action there rather showing alerts.

A prompt alert example

In this demo, a prompt alert is shown to take the user input before proceeding.

JavaScript prompt alert

See online demo and code

Setting up this cool plug-in

In order to use the SweetAlert plug-in on your website, you simply need to include two dependency files in the <head> section of your webpage:

 <script src=” sweetalert-dev.js”></script>

<link rel=”stylesheet” href=” sweetalert.css”>

Get the files from the GitHub website. You may place these files in folders as per your project specifications. The downloaded package contains many other files but only two files are required as mentioned earlier and used in demos.

This div height required for enabling the sticky sidebar