Skip to content
Subscribe to RSS Find me on GitHub Follow me on Twitter

Building a Yes No Dialog with Javascript


A yes/no dialog is a type of user interface prompt that requires the user to choose between two options: "yes" or "no". It is commonly used to confirm or reject an action before proceeding.

In this guide, we will focus on building a yes/no dialog using modals in combination with HTML and Javascript. Modals are a popular way to create pop-up windows or dialogs in web applications.

This guide is suitable for beginners who have a basic understanding of HTML, CSS, and Javascript. If you are new to these technologies, don't worry - we will provide the necessary resources to get you up to speed. Let's get started!


To successfully build a yes/no dialog with Javascript, it is important to have a basic understanding of HTML, CSS, and Javascript. If you are new to these technologies or need a refresher, you can check out the following resources:

Setting Up the HTML

To create a basic HTML structure for the yes/no dialog, follow these steps:

  1. Create a <div> element with a unique ID to serve as the modal container. For example:
<div id="modal-container">
    <!-- modal content goes here -->
  1. Inside the modal container, create a <div> element to represent the modal itself. Add a class to style it as a modal. For example:
<div id="modal-container">
    <div class="modal">
        <!-- modal content goes here -->
  1. Within the modal <div>, include the necessary elements for the yes/no dialog such as the question and buttons. For example:
<div id="modal-container">
    <div class="modal">
        <p>Are you sure you want to proceed?</p>
        <button id="yes-button">Yes</button>
        <button id="no-button">No</button>

Make sure to customize the content and styling according to your specific needs.

Styling the Modal

To style the modal, you can use CSS. Here are the steps to style the modal:

  1. Select the modal element using its class or ID.
  2. Set the desired background color, font properties, dimensions, and positioning properties.
  3. Customize the appearance of the modal by adding borders, box shadows, and animations.
  4. Style the buttons for the yes and no options.

Here's an example of CSS code that styles a modal:

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffff;
  box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.3);
  width: 300px;
  padding: 20px;
  border-radius: 5px;

.modal h2 {
  font-size: 20px;
  margin-bottom: 20px;

.modal button {
  padding: 10px 20px;
  margin-right: 10px;
  cursor: pointer;

.modal button.yes {
  background-color: #27ae60;
  color: #ffffff;

.modal {
  background-color: #c0392b;
  color: #ffffff;

Feel free to customize the CSS code to match your desired styling preferences.

Adding Functionality with Javascript

To add functionality to the yes/no dialog using Javascript, follow these steps:

Step 1: Select the modal element

  • Use the querySelector method to select the modal element in your HTML.
  • Assign it to a variable for easier manipulation in Javascript.

Step 2: Create functions for showing and hiding the modal

  • Define a function named showModal that will display the modal.
  • Inside the function, use the style property to set the display property of the modal element to 'block'.
function showModal() {
  var modal = document.querySelector('.modal'); = 'block';
  • Define another function named hideModal that will hide the modal.
  • Inside the function, set the display property of the modal element to 'none'.
function hideModal() {
  var modal = document.querySelector('.modal'); = 'none';

Step 3: Trigger the functions with event listeners

  • Select the elements that will trigger the showing and hiding of the modal, such as buttons or links.
  • Add event listeners to those elements and assign them to the corresponding functions.
var showButton = document.querySelector('#show-modal-button');
showButton.addEventListener('click', showModal);

var hideButton = document.querySelector('#hide-modal-button');
hideButton.addEventListener('click', hideModal);

Make sure to replace 'show-modal-button' and 'hide-modal-button' with the appropriate IDs or class names of your elements.

By following these steps, you can add functionality to your yes/no dialog using Javascript. Use these foundation concepts to build more complex interactions and customize the dialog according to your requirements.


In this guide, we covered the process of building a Yes/No dialog with Javascript. We started by setting up the HTML structure for the dialog and then moved on to styling it using CSS. Finally, we added the necessary functionality with Javascript to show and hide the modal.

It is important to practice and experiment with modals in your own projects to become comfortable with implementing them. By doing so, you will gain a better understanding of how modals work and how they can enhance user interactions.

To further enhance your knowledge and skills in Javascript and web development, here are some additional resources for learning and reference:

Happy coding and have fun building amazing yes/no dialogs for your projects!