JavaScript 💬 Dialog boxes - alert(), confirm() and prompt()

Share on email
Share on twitter
Share on linkedin
Share on facebook
Article URLs in JavaScript by @wpr0ck
Article URLs in JavaScript by @wpr0ck
Article URLs in JavaScript by @wpr0ck
Article URLs in JavaScript by @wpr0ck
Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email

JavaScript provides default dialog boxes that allow you to interact with the user. There are 3 types of dialog boxes. Alerts() which allow you to display a message. Confirmations() which allow you to retrieve a Boolean value and prompts() which allow you to retrieve a text value.

JavaScript dialog box: alert()

The dialog box that displays a text and an "OK" button.

alert("Message to display");
JavaScript Alert : Result of the alert() function

JavaScript dialog box: confirm()

The confirm() dialog box displays a message and two buttons: an "OK" button and a "Cancel" button. This function returns a Boolean value that is set to "true" if the OK button is clicked and returns "false" if the "Cancel" button is clicked.

if ( confirm( "Message à afficher" ) ) {
    // Code à éxécuter si le l'utilisateur clique sur "OK"
} else {
    // Code à éxécuter si l'utilisateur clique sur "Annuler" 
}
Confirm JavaScript: Result of the confirm() function

😎 The high-end WordPress host everyone's talking about

JavaScript dialog box: prompt()

The prompt() dialog box displays a message, a field to fill (text type imput) and an "OK" button. This function returns the value that was entered in the field by the user.

var value = prompt("Message to display");
Prompt JavaScript: Result of the prompt() function

Julien MA Jacob
WordPress Developer
Julien MA JACOB - WordPress Developer
Passionate about the web and programming with always many projects in mind
  Discussion of the article
Subscribe to the newsletter
Notify to
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
didjess

Excellent! Excellent!

Fred

Hello,

Thank you for your article. Is it possible to do the same for pages? If Yes, what would be the adaptations to be made in your starting code?