Your title goes here

How TO — Alerts

Alert messages can be used to notify the user about something special: danger, success, information or warning.

Create An Alert Message

Step 1) Add HTML:

Example

If you want the ability to close the alert message, add a element with an onclick attribute that says «when you click on me, hide my parent element» — which is the container (class=»alert»).

Tip: Use the HTML entity » × » to create the letter «x».

Step 2) Add CSS:

Style the alert box and the close button:

Example

/* The alert message box */
.alert padding: 20px;
background-color: #f44336; /* Red */
color: white;
margin-bottom: 15px;
>

/* The close button */
.closebtn margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
>

Читайте также:  Class with method python

/* When moving the mouse over the close button */
.closebtn:hover color: black;
>

Many Alerts

If you have many alert messages on a page, you can add the following script to close different alerts without using the onclick attribute on each element.

And, if you want the alerts to slowly fade out when you click on them, add opacity and transition to the alert class:

Example

// Get all elements with >var close = document.getElementsByClassName(«closebtn»);
var i;

// Loop through all close buttons
for (i = 0; i < close.length; i++) // When someone clicks on a close button
close[i].onclick = function()

// Get the parent of var div = this.parentElement;

// Set the opacity of div to 0 (transparent)
div.style.opacity = «0»;

// Hide the div after 600ms (the same amount of milliseconds it takes to fade out)
setTimeout(function()< div.style.display = "none"; >, 600);
>
>

Tip: Also check out Notes.

Источник

Create JavaScript Alert Box With Three Buttons

The JavaScript alert box has only one button which is the ok button. Alert box With two buttons ok and cancel is known as Confirmation Dialog Box. In this tutorial, We will learn how to create an alert box with three buttons in JavaScript, i.e Create JavaScript alert box with three buttons –yes no and cancel

You can change the button text too.

This tutorial is an older one and this method does not work in newer browsers.

Confirmation Box with three buttons in JavaScript

You can create the confirmation box with three buttons in two methods:

We will use jQuery to create alert box with three buttons. But we will not use any custom CSS. We will use jQuery UI CSS

Читайте также:  Ссылка для печати html

We will use jQuery but we will do this with our custom CSS

Create JavaScript Alert Box with three buttons with jQuery UI CSS

If the user clicks on the button, a confirmation box will appear like this:

Alert box with three buttons in JavaScript

JavaScript Alert Box with three buttons with Custom CSS

      #confirm < display: none; background-color: pink; border: 1px solid #aaa; position: fixed; width: 250px; left: 50%; margin-left: -100px; padding: 6px 8px 8px; box-sizing: border-box; text-align: center; >#confirm button < background-color: lavender; display: inline-block; border-radius: 5px; border: 1px solid #aaa; padding: 5px; text-align: center; width: 80px; cursor: pointer; >#confirm .message  

Confirmation Box with three buttons in JavaScript

I hope this was useful to you. If you have any question or doubt regarding this tutorial feel free to comment in the below comment section.

One response to “Create JavaScript Alert Box With Three Buttons”

.masterGlobalLayer <
padding: 4px 20px 10px 20px;
background-image: url(tile.png);
background-repeat: repeat;
background-position: top left;
bottom: 0;
>.masterGlobalLayer <
padding: 4px 20px 10px 20px;
background-image: url(tile.png);
background-repeat: repeat;
background-position: top left;
bottom: 0;
>

Источник

JavaScript Popup Boxes

JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.

Alert Box

An alert box is often used if you want to make sure information comes through to the user.

When an alert box pops up, the user will have to click «OK» to proceed.

Syntax

The window.alert() method can be written without the window prefix.

Example

Confirm Box

A confirm box is often used if you want the user to verify or accept something.

When a confirm box pops up, the user will have to click either «OK» or «Cancel» to proceed.

If the user clicks «OK», the box returns true. If the user clicks «Cancel», the box returns false.

Syntax

The window.confirm() method can be written without the window prefix.

Example

Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either «OK» or «Cancel» to proceed after entering an input value.

If the user clicks «OK» the box returns the input value. If the user clicks «Cancel» the box returns null.

Syntax

The window.prompt() method can be written without the window prefix.

Example

let person = prompt(«Please enter your name», «Harry Potter»);
let text;
if (person == null || person == «») text = «User cancelled the prompt.»;
> else text = «Hello » + person + «! How are you today?»;
>

Line Breaks

To display line breaks inside a popup box, use a back-slash followed by the character n.

Источник

JavaScript Alert [Examples And Usage]

Note that, when executed, the alert function will get the focus and prevent the user from interacting with the reset of the website until the modal gets dismisssed.

Examples using alert

1. Alert message on click

A very common use case for the alert fuction is to use it when filling a form and then trying to submit it by clicking a button.

Let’s say we want to make sure the user is over 18 years old when filling the following form:

form name="myForm" action=""> 
label for="age">Agelabel>
input type="number" name="age" id="age" />
button type="submit">Submitbutton>
form>

All we have to do is attach an event listener to the button, checking for the value of the input field and then displaying the modal with the alert function of JavaScript:

var checkAge = (e) =>  
if(document.querySelector('#age').value 18)

// Preventing the submit of the form
e.preventDefault();

// Displaying the modal window
alert("You have to be older 18!");
>
>;

// Listening to the click event on the button
document.querySelector('button').addEventListener('click', checkAge);

2. Alert box before closing the window

It’s also a common practise in websites that require saving changes to display an alert message when the user tries to close the browser’s window or tab.

To do this, we have to first detect when the user is about to close the window. We can achieve this in two different ways, but it’s usually recommended to use event listeners when possible.

// Using event listeners
window.addEventListener("beforeunload", showDialog);

// Overwriting the window default property function
window.onbeforeunload = showDialog;

Now all we have to do is show them a dialog. But in this case, we won’t be using the alert function for this scenario.

Unfortunately we can not customise the message that gets displayed on the dialog anymore. We still have to return a text for old browsers compatibility, but the message will most likely not get displayed on modern browsers. They’ll show a default message telling the user that changes won’t get saved.

So, here’s how we tell the browser we want to show a message, but returning a string on the event function:

var showDialog = (e) =>  
return 'Dialog text here.';
>;
window.addEventListener("beforeunload", showDialog);

3. Alert box on page load

In some very specific cases, websites might want to show an alert message on page load. To do this all we need to do is fire the alert message on the section of our HTML. This way, the alert will be shown before loading any elements on the page.

head> 
script>
alert("Displayed before page loads");
/script>
/head>
body>
Your page content.
/body>

4. Alert message using a variable

Using a variable to display different messages based on its content can also be done quite easily. Simply pass the variable to the alert method instead of a string text.

var myVariable = 'I love alert boxes!!';
alert(myVariable);

5. Alert a message requiring confirmation

Perhaps you want to show an alert that requires the visitors confirmation and that allows them too also cancel or ignore it by displaying a «Cancel» and an «OK» buttons. For these cases we will use the confirm function, which can be seen as a variant of the alert function.

The confirm function will just add a «Cancel» button on top of the «OK» one added by the alert method.

confirm('Do you want to continue?');

We can catch the visitor’s decision by checking the result of the method. It will be true if the visitor clicks on «OK» and false otherwise.

if(confirm("Do you want to continue?")) 
// continue heree
>

6. Alert a message showing input field

In some occassions we might want to capture the user input directly on the alert message. In this cases we will also move away from the alert function and use the alternative function prompt . This function creates the same dialog box but adds a text field to it and the «Cancel» button too.

It admits twoo parameters. The first one is the text to show on the dialog (for the input) and the second one is the default value for the input (if any).

// Showing a dialog box with a text input containing "Steve"
prompt("Enter your name, please:", "Steve");

// Showing a dialog box with ab empty text input.
prompt("Enter your name, please:");

7. Alerts with custom styles

In order to use your own custom alert modals we’ll have to make use of JavaScript and CSS. The easiest, fastest and probably most reliable way to do it is by using external plugins instead of doing it by yourself.

One of the most used ones is SweetAlert2 , or even its predecesor SweetAlert.

Here’s an example of how a confirm alert dialog looks like when using this component:

var showConfirm = (e) =>  
e.preventDefault();

Swal.fire(
title: 'Error!',
text: 'Do you want to continue',
confirmButtonText: 'Yeap!'
>);
>;

document.querySelector('button').addEventListener('click', showConfirm);

As you can see there aren’t huge changes in our code and the usage is quite straight away.

You can check more alert examples using this component on their website.

There are other dialog boxes plugins out there, so feel free to choose the one that fits your needs.

Источник

Оцените статью