Append the dialog element to the body using document.body.appendChild(myDialog)

function Popup() {
  var myDialog = document.createElement("dialog");
  document.body.appendChild(myDialog)
  var text = document.createTextNode("This is a dialog window");
  myDialog.appendChild(text);
  myDialog.showModal();
}
<button onclick="Popup()" role='button'>
    Show Alert
</button>

Answer from brk on Stack Overflow
🌐
GitHub
gist.github.com › saksoy › 8498802
Popup Modal.html · GitHub
Sign in Sign up · You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert · {{ message }} Instantly share code, notes, and snippets. Forked from joshwentz/Popup Modal.html ·
🌐
W3Schools
w3schools.com › howto › howto_css_modals.asp
W3Schools.com
Google Charts Google Fonts Google ... to create a Modal Box with CSS and JavaScript. A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal...
Discussions

html - Is it possible to create modal popup only from javascript - Stack Overflow
So I have an index.html that I cant edit. Only thing I can use is a javascript file that is linked to that index file. Modal is supposed to be created through javascript (through createElement meth... More on stackoverflow.com
🌐 stackoverflow.com
How to make modal popup after button pressed
I went online and some research on how i could make my sign up modal pop up once someone presses the signup button n my navbar, however i was unable to come across any information on how to do that... More on stackoverflow.com
🌐 stackoverflow.com
Creating a modal using CSS, using buttons to trigger the modal popup
Swapnil Srivastava is having issues with: Hi, I wanted open a modal popup using the CSS only, using the class of the button I want to trigger the modal pop up. I have t... More on teamtreehouse.com
🌐 teamtreehouse.com
4
April 12, 2016
html - how to write a modal popup in javascript - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I have a table that displays data along side with a button, I want the button to display a modal popup ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Bootstrap
getbootstrap.com › docs › 4.0 › components › modal
Modal · Bootstrap
Use Bootstrap’s JavaScript modal plugin to add dialogs to your site for lightboxes, user notifications, or completely custom content.
🌐
W3Schools
w3schools.com › w3css › w3css_modal.asp
W3.CSS Modal
Web Intro Web HTML Web CSS Web ... W3.CSS Downloads · ❮ Previous Next ❯ · A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal ·...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-build-a-modal-with-javascript
How to Build a Modal with JavaScript
October 3, 2022 - By Victor Eke It's probably happened to you before: you unintentionally attempted to perform an action on a webpage, but luckily got a pop-up window asking you to confirm your decision. This pop-up window is called a modal. It's a web page element t...
🌐
W3Schools
w3schools.com › bootstrap › bootstrap_modal.asp
Bootstrap 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
Find elsewhere
🌐
YouTube
youtube.com › watch
dialog = the easiest way to make a popup modal - YouTube
Ever needed to make a modal? Well, we have a native HTML element that does the job with the dialog element! It does a lot of things out of the box that are r...
Published   March 17, 2022
🌐
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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Element › dialog
<dialog>: The Dialog element - HTML - MDN Web Docs - Mozilla
Note that, because the dialog is a popover, it will be non-modal, so you can close it by clicking outside the dialog. The HTML below shows how to apply the attributes to a <button> element so it can be pressed to show and hide a modal <dialog> with an id of "my-dialog".
🌐
CodingNepal
codingnepalweb.com › home › card design › create popup modal box in html css & javascript
Create Popup Modal Box in HTML CSS & JavaScript
May 13, 2023 - As you have seen in the video of this project [ Popup Modal Box]. At first, there was a button on the screen with the name “Open Modal”, When I clicked on that button the button disappeared and a modal box, as well as a little dark color overly behind the modal box, appeared. As you the modal box can be closed by clicking on the close button or just by clicking on the outside of the modal box. To make the UI design of this project [ Popup Modal Box], I just used HTML and CSS, and to open and close it, I used some JavaScript code.
🌐
Medium
medium.com › @rjgorz › creating-a-modal-popup-html-css-vanilla-js-ec759500d479
Creating A Modal “Popup” || HTML/CSS/Vanilla JS | by Robert Gorz | Medium
January 26, 2023 - To render the content and make the modal appear in the foreground, I utilized click event listeners on several elements within the page. Upon clicking these elements, the modal becomes visible and animates out of the top of the screen!
🌐
Adobe Support Community
community.adobe.com › questions-621 › html-css-modal-popup-problems-642756
HTML CSS Modal Popup Problems | Community
August 2, 2021 - <!DOCTYPE html> <html> <head> <!-- --------------- IF I LINK TO EXTERNAL STYLESHEET BUTTON DISAPPEARS --> <!-- <link href="css/global.css" rel="stylesheet" type="text/css" /> --> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body { font-family: Arial, Helvetica, sans-serif; } /* The Modal (background) */ .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ padding-top: 100px; /* Location of the box */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Ena
🌐
Team Treehouse
teamtreehouse.com › community › creating-a-modal-using-css-using-buttons-to-trigger-the-modal-popup
Creating a modal using CSS, using buttons to trigger the modal popup (Example) | Treehouse Community
April 12, 2016 - html code <button type="submit" class="btn-action">Add to bag </button> could you relook once again to find a better alternative , Thanks Alexandre for the code. on the contrary , if I could use to jquery to inject a class or something to manipulate the code. in order to reflect the desired outcome. ... Have you checked out our workshop on creating a modal window with CSS?
🌐
Envato Tuts+
webdesign.tutsplus.com › home › web design › html/css › animation
How to Build a JavaScript Popup Modal From Scratch | Envato Tuts+
November 3, 2023 - In this tutorial we’ll learn how to build JavaScript modals (popup windows) without using a framework like Bootstrap, or a third party library. We’ll build the whole thing from scratch, giving us complete control over how it works and looks. ... Please accept marketing cookies to load this content. First, we’ll create a modal. To do this, we’ll add the .modal class and a unique ID to a container.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Reference › Selectors › :modal
:modal - CSS | MDN
December 16, 2025 - <!-- Basic modal dialog containing a form --> <dialog id="favDialog"> <form method="dialog"> <p> <label >Favorite animal: <select> <option value="default">Choose…</option> <option>Brine shrimp</option> <option>Red panda</option> <option>Spider monkey</option> </select> </label> </p> <div> <button value="cancel">Cancel</button> <button id="confirmBtn" value="default">Confirm</button> </div> </form> </dialog> <p> <button id="updateDetails">Update details</button> </p> <output></output>
🌐
React Native
reactnative.dev › docs › modal
Modal · React Native
3 weeks ago - The Modal component is a basic way to present content above an enclosing view.
🌐
Medium
matemarschalko.medium.com › lets-build-a-html-and-css-only-popup-modal-5bf26ec62c7a
Lets build a HTML- and CSS-only Popup Modal | by Mate Marschalko | Medium
December 6, 2024 - The checkbox is controlled by the label which should now look like a button and have a blue colour. All we have left to to do is create the modal box that will appear when the checkbox is checked: ... Let’s add the HTML and CSS for this modal first, then we can worry about the hide and show logic later:
Top answer
1 of 1
1

Your example code isn't just regular JavaScript, it looks like jQuery and it looks like you are trying to use a jQuery modal plugin. If that's the route you want to go, you need to do some setup that depends on your specific situation. Your code and setup is so complex and specific that I recommend learning more fundamentals before proceeding OR rebuilding this specific part of your application.

Also; you are using IDs to target your elements, but you have multiple elements with the same ID, which should never happen.

Below I have a tiny demo of a custom "modal" type thing using just HTML, CSS and vanilla (meaning plain) JS without jQuery

function toggleModal(e) {
    e.stopPropagation()
    backdrop.classList.toggle("active")
}
const backdrop = document.querySelector(".backdrop")
const button = document.querySelector("button")
backdrop.addEventListener("click", toggleModal)
button.addEventListener("click", toggleModal)
.backdrop {
  display: none;
  position: fixed;
  width:100vw;
  height:100vh;
  cursor:pointer;
  background:#00000088;
  top: 0;
}
.backdrop.active {
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal {
  background: #ffffff;
  min-width:30vw;
  min-height: 30vh;
  cursor:initial;
}
<button>OpenModal</button>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam vel consectetur ex, id pharetra ex. In ligula leo, euismod eu gravida sed, facilisis non dolor. In hac habitasse platea dictumst. Aenean sapien dolor, aliquam quis odio a, feugiat rutrum turpis. Duis eu eleifend urna. Fusce quis quam hendrerit, varius nibh sed, tempor erat. Fusce iaculis finibus lacus in fringilla. Nullam tincidunt odio eu efficitur lacinia. Vivamus id tempus mauris, in laoreet magna. Integer ornare, odio quis ullamcorper ultrices, purus nisl elementum erat, id semper eros nunc et orci. Morbi neque purus, fringilla ut rutrum quis, commodo vel arcu.</p>
<p>Integer sit amet volutpat ante. Donec sed sem non nisi hendrerit volutpat ac vel ante. Suspendisse nec nulla interdum, aliquam mauris non, porta elit. Suspendisse potenti. Proin placerat tincidunt malesuada. Nullam accumsan mi finibus orci pretium pharetra. Nunc at efficitur leo, sit amet ultrices arcu. Aenean non nibh congue, efficitur justo et, posuere ante. Phasellus maximus augue at pulvinar pulvinar. Integer tempor, orci eu pellentesque finibus, enim tortor hendrerit dui, quis semper lorem dolor ac nulla. Mauris congue venenatis blandit. Pellentesque a mi in odio venenatis iaculis vel non elit.</p>

<div class="backdrop">
  <div class="modal">
    I am a modal
  </div>
</div>