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

Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email
Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email

JavaScript offers default dialog boxes that allow to interact with the user. There are 3 types of dialog boxes. The alerts() which allow to display a message. The confirm() which allow to retrieve a boolean value and the prompt() which allow to retrieve a text value.

JavaScript dialog box: alert()

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

alert ("Message to be displayed");
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 which is "true" if it is the OK button which is clicked and returns "false" if it is the "Undo" button which is clicked.

if ( confirm( "Message to display" ) ) {
    // Code to be executed if the user clicks on "OK
} else {
    // Code to be executed if the user clicks on "Cancel" } else { 
}
Confirm JavaScript : Result of the function confirm()

JavaScript dialog box: prompt()

The prompt() dialog box displays a message, a field to fill in (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 be displayed);
Prompt JavaScript : Result of the prompt() function

Julien .MA Jacob
Developer WordPress
Julien MA Jacob - Developer WordPress
🧙♂️ Passionate about the web and programming with always lots of projects in mind 👨💻 | About page
  Discussion of the article
Subscribe to our newsletter
Notify of
guest
2 Comments
most popular
latest oldest
Inline Feedbacks
View all comments
didjess

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?