🌐
W3Schools
w3schools.com › howto › howto_css_modals.asp
How To Make a Modal Box With CSS and JavaScript
Fullscreen Video Modal Boxes Delete Modal Timeline Scroll Indicator Progress Bars Skill Bar Range Sliders Color Picker Email Field Tooltips Display Element Hover Popups Collapsible Calendar HTML Includes To Do List Loaders Badges Star Rating User Rating Overlay Effect Contact Chips Cards Flip Card Profile Card Product Card Alerts Callout Notes Labels Ribbon Tag Cloud Circles Style HR Coupon List Group List Group with Badges List Without Bullets Responsive Text Cutout Text Glowing Text Fixed Footer Sticky Element Equal Height Clearfix Responsive Floats Snackbar Fullscreen Window Scroll Drawing
🌐
W3Schools
w3schools.com › w3css › w3css_modal.asp
W3.CSS Modal
You can also close it by clicking outside of the modal (see example below). Tip: × is the preferred HTML entity for close icons, rather than the letter "x". Use w3-container classes to create different sections inside the modal content: Open Modal with Containers
🌐
W3Schools
w3schools.com › bootstrap › bootstrap_modal.asp
Bootstrap Modal Plugin
The <button> inside the header has a data-dismiss="modal" attribute which closes the modal if you click on it. The .close class styles the close button, and the .modal-title class styles the header with a proper line-height. The .modal-body class is used to define the style for the body of the modal. Add any HTML markup here; paragraphs, images, videos, etc.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › dialog
<dialog>: The Dialog element - HTML | MDN
Note: While you can toggle between the open and closed states of non-modal dialog boxes by toggling the presence of the open attribute, this approach is not recommended. See open for more information. The HTML <dialog> element is used to create both modal and non-modal dialog boxes.
🌐
Bootstrap
getbootstrap.com › docs › 4.0 › components › modal
Modal · Bootstrap
Modals are built with HTML, CSS, and JavaScript. They’re positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › :modal
:modal - CSS | MDN
:modal { border: 5px solid red; background-color: yellow; box-shadow: 3px 3px 10px rgb(0 0 0 / 50%); }
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › components › modal
Modal · Bootstrap v5.0
Modals are built with HTML, CSS, and JavaScript. They’re positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-build-a-modal-with-javascript
How to Build a Modal with JavaScript
October 3, 2022 - The button with the class of btn and btn-open will serve as your modal button so it fires up our modal when you click this button. Now inside of your modal, add the markup, and also add the X button for closing the modal.
Top answer
1 of 2
1

After reading your answers, I noticed my mistake:

...
<div class="modal modal-content" id="seeModal">
    <span class="close">&times;</span>
...

was supposed to be the same as

...
<div class="modal modal-content" id="hearModal">
    <span class="close" id="close">&times;</span>
...

Fixing that and defining var close inside the function did the trick! Thank you!

2 of 2
0

In your code, you have id="close" for the close button in the "hearModal". However, when you're trying to select it in JavaScript, you're using const closeModal = document.getElementById("close");, which only selects the first element with the ID of "close", which is associated with the "hearModal" close button.

Check the below example, this may get you some idea.

const seeModal = document.getElementById("seeModal");
const hearModal = document.getElementById("hearModal");

const seeBtn = document.getElementById("seeBtn");
const hearBtn = document.getElementById("hearBtn");

const seeCloseModal = document.getElementById("seeClose");
const hearCloseModal = document.getElementById("hearClose");

const closeSeeModal = function () {
    seeModal.style.display = "none";
};

const closeHearModal = function () {
    hearModal.style.display = "none";
};

seeBtn.addEventListener("click", function () {
    seeModal.style.display = "block";
});

hearBtn.addEventListener("click", function () {
    hearModal.style.display = "block";
});

seeCloseModal.addEventListener("click", closeSeeModal);
hearCloseModal.addEventListener("click", closeHearModal);
.modal {
    display: none; /* Hidden by default */
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0,0,0,0.5);
}

.modal-content {
    background-color: #fff;
    margin: 15% auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
    max-width: 600px;
}

.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

    .close:hover,
    .close:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
    }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Modal Example</title>
</head>
<body>

    <!-- Trigger/Open The Modal -->
    <p><button id="seeBtn" type="button">SEE</button></p>
    <p><button id="hearBtn" type="button">HEAR</button></p>

    <!-- The Modal -->
    <div class="modal" id="seeModal">
        <div class="modal-content">
            <span class="close" id="seeClose">&times;</span>
            <p>See their profile</p>
            <p>Additional content...</p>
        </div>
    </div>

    <div class="modal" id="hearModal">
        <div class="modal-content">
            <span class="close" id="hearClose">&times;</span>
            <p>Hear their Stories</p>
            <p>Additional content...</p>
        </div>
    </div>
</body>
</html>

Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLDialogElement › showModal
HTMLDialogElement: showModal() method - Web APIs | MDN
The showModal() method of the HTMLDialogElement interface displays the dialog as a modal dialog, over the top of any other dialogs or elements that might be visible.
🌐
W3Schools
w3schools.com › css › css3_images_modal.asp
CSS Responsive Modal Images
CSS Templates CSS Examples CSS ... Values CSS Browser Support ... A modal image gives a user the ability to display a larger version of an image, without navigating away from the current page....
🌐
Medium
medium.com › @idorenyinudoh10 › mastering-modals-a-comprehensive-guide-to-building-modals-with-the-html-dialog-element-ae64a3c2bab7
Mastering Modals: A Comprehensive Guide to Building Modals with the HTML <dialog> Element
June 27, 2023 - MDN describes it as a dialog box or other interactive component, such as a dismissible alert, inspector, or subwindow. The <dialog> element, a semantic HTML element, has an open attribute that hints to the browser that the modal is active. Also, the HTMLDialogElement interface provides methods that help manipulate the <dialog> element.
🌐
Medium
sahil2004.medium.com › dialogs-cf832e2f583c
New way to create modals using HTML only | by Sahil Garg | Medium
December 20, 2024 - Hence, we can implement immediately in our production sites. A dialog/modal, is a popup that appears on a button click. To create it, we have the <dialog> HTML tag. But there is a difference between a dialog and a modal.
🌐
Web Dev Simplified
blog.webdevsimplified.com › 2023-04 › html-dialog
Modals Will Never Be The Same - HTML dialog Element
The main difference between a dialog and a modal is that a modal is supposed to take complete priority over the page and prevent the user from interacting with the page until the modal is closed while a dialog, is just a popup that doesn’t take complete priority over the page and should allow the user to interact with the page while the dialog is open.
🌐
Medium
medium.com › @nerdplusdog › a-how-to-guide-for-modal-boxes-with-javascript-html-and-css-6a49d063987e
A How-To Guide for Modal Boxes with Javascript, HTML, and CSS | by Gabbie Piraino | Medium
February 12, 2019 - In the above classes, we have styling for each of the elements that we created in our HTML. The .modal class itself has attributes that determine how it treats the page behind the modal itself: a width and height in comparison to the page behind the dialog box, a color (this example uses a semi-opaque gray) and a visibility setting so that we can toggle it in our Javascript when a user clicks on the button.
🌐
W3Schools
w3schools.com › bootstrap4 › bootstrap_modal.asp
Bootstrap 4 Modals
For a complete reference of all modal options, methods and events, go to our Bootstrap JS Modal Reference. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-create-a-modal-box-using-html-css-and-javascript
How to Create a Modal Box using HTML CSS and JavaScript? - GeeksforGeeks
We will learn how to create a modal dialog box using HTML, CSS, and JavaScript. A modal is a pop-up dialog that appears on top of the main content and requires the user to interact with it before returning to the main screen.
Published   October 10, 2024
🌐
W3Schools
w3schools.com › bootstrap › bootstrap_ref_js_modal.asp
Bootstrap JS Modal Reference
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
Materialize
materializecss.com › modals.html
Modals - Materialize
Modal · Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.